我正在尝试学习this book,但所有示例都没有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'
有人可以向我解释为什么会发生这种情况吗?
答案 0 :(得分:1)
首先你不能添加
UIViewController
作为子视图
方法
addSubview:(UIView *)
需要UIview并且您正在添加UIViewController。
其次,在iOS 4及更早版本中使用了添加子视图, 现在,您将视图控制器添加为rootviewcontroller。
所以简单地添加,
self.window.rootViewController = myView
如果您想使用addSubview,则需要执行此操作
[self.window addSubView:myView.view]
答案 1 :(得分:1)
转到AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
return YES;
}
答案 2 :(得分:0)
这样做了。转到AppDelegate.m并转到 - (BOOL)应用程序:(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];
答案 3 :(得分: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
}
答案 4 :(得分: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;
}
答案 5 :(得分:-1)
您需要设置: -
在AppDelegate.m文件中:null
_applicationDidFinishLaunchingWithOptions_