-1

我正在尝试学习这本书,但所有示例都没有storyBoard,当我尝试构建应用程序时,我发现了这个错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'

我试图用这个来修复它:

ViewController *myView = [[ViewController alloc] init];
[self.window addSubview:myView];
self.window.rootViewController = myView;

但后来我收到另一个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HypnoView _preferredInterfaceOrientationGivenCurrentOrientation:]: unrecognized selector sent to instance 0x78f68730'

有人可以向我解释为什么会发生在我身上吗?

4

6 回答 6

1

转到 AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window.rootViewController = self.viewController;
    return YES;
}
于 2016-05-04T12:02:32.863 回答
1

首先,您不能添加

UIViewController 

作为子视图

方法

addSubview:(UIView *)

需要一个 UIview 并且您正在添加一个 UIViewController。

其次添加子视图在 iOS 4 及更早版本中使用,现在您添加一个视图控制器作为 rootviewcontroller。

所以简单地添加,

self.window.rootViewController = myView

如果你想使用 addSubview,你需要这样做

[self.window addSubView:myView.view]
于 2016-05-04T11:58:15.160 回答
0

目标-c

@interface AppDelegate () 
    @property (nonatomic, strong) ViewController *viewController;
@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self setViewController:[[ViewController alloc] init]];

    [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
    [[self window] setBackgroundColor:[UIColor redColor];
    [[self window] setRootViewController:[self viewController]];
    [[self window] makeKeyAndVisible];

    return YES;
}

迅速

var window: UIWindow?
let viewController = ViewController();

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window!.backgroundColor = UIColor.redColor()
    self.window!.rootViewController = playerController
    self.window!.makeKeyAndVisible()

    return true
}
于 2016-05-04T12:47:40.910 回答
0

它是这样做的。转到 AppDelegate.m 并转到 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

ViewController *myView = [[ViewController alloc] init];
self.window.rootViewController = myView;
[self.window makeKeyAndVisible];

如果要将 UIView 添加为子视图。然后执行以下操作:

UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
[self.window addSubview:view];
于 2016-05-04T12:39:51.590 回答
0

试试这个代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

ViewController *controller = [[ViewController alloc]initWithNibName:@"View" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
于 2016-05-04T12:54:31.770 回答
-1

您需要设置:-

在 AppDelegate.m 文件中:_applicationDidFinishLaunchingWithOptions_

UIView *myView;
    self.window.rootViewController = myView

        myView= self.viewController;
        [self.window makeKeyAndVisible];
于 2016-05-04T11:57:53.287 回答