设置Windows根视图控制器不起作用

时间:2014-03-22 01:20:32

标签: ios objective-c uiviewcontroller

我正在尝试从我的AppDelegate更改视图控制器,如下所示:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyBoard instantiateInitialViewController];
NSLog(@"ViewController: %@",vc);
self.window.rootViewController = vc;
NSLog(@"ViewController: %@",self.window.rootViewController);

然而,屏幕仍为黑色。这是我的NSLogs:

ViewController: MyViewController
ViewController: (null)

2 个答案:

答案 0 :(得分:10)

看来你的self.window是零。你确定你没有忘记创造它吗?

self.window = [ [ UIWindow alloc ] initWithFrame:[ [ UIScreen mainScreen ] bounds ] ];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

答案 1 :(得分:3)

您可以通过将箭头拖动到所需的ViewController来更改故事板中的根视图控制器而无需编写任何代码: enter image description here