ios – 通过AppDelegate以Storyboard方式实例化View Controller

我正在建立一个应用程序 – 当第一次启动它时,它要求用户做两件事情:

>选择国家
>接受T& Cs

从那里到家庭视图控制器.

我目前面临的问题是将第一个视图控制器从我的应用程序代理推送到屏幕上.我正在使用storyboards / Xcode 5 / iOS7

这是我提出的代码

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
BBCounterySettingsViewController *controller = (BBCounterySettingsViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"CountrySettings"];
[navigationController pushViewController:controller animated:NO];

问题是应用程序崩溃时遇到以下错误的最后一行代码

* Terminating app due to uncaught exception ‘NSinvalidargumentexception’,reason: ‘-[UIViewController
pushViewController:animated:]: unrecognized selector sent to instance
0x8e9a400’

任何人有什么想法我做错了什么?

解决方法

你期待self.window.rootViewController是一个UINavigationController,但它是一个UIViewController.这意味着你的故事板中的最外面的视图控制器的类型是错误的.

获取UINavigationController(在这种情况下应该工作)的更好的方法是在任何UIViewController上使用属性self.navigationController.

根据我的理解,您希望在用户第一次运行时让用户选择一些东西.你应该做的是呈现一个模态视图控制器,像这样:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//isFirstRun is some boolean you use to determine if it's the first run
if(isFirstRun){
    BBCounterySettingsViewController *controller = (BBCounterySettingsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"CountrySettings"];
    [self.window.rootViewController presentViewController: controller animated:YES completion:nil];
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...