在Appdelegate的Storyboard中以编程方式为UINavigationcontroller设置rootViewcontroller [英] Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate
问题描述
我在NSUserdefaults
中有一个值.我正在使用storyboard
,它嵌入在UINavigationController
中.
I have a value in NSUserdefaults
. I am using storyboard
, it is embedded in UINavigationController
.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[NSUserDefaults standardUserDefaults]objectForKey:@"isLoggedIn"]){
//show home page here
}else{
// show login view
}
}
我也可以使用URL
打开应用
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if(text.length > 0){
// show home page
}else {
// show settings page
}
return YES;
}
如何根据检索到的值为UINavigationController
设置rootViewController
.有人可以帮帮我吗?
How can I set the rootViewController
for the UINavigationController
based on the value that is retrieved .Can anyone please help me out?
推荐答案
您可以按照if/else条件使用ViewController创建UINavigationController的对象,并将导航控制器设置为AppDelegate中window的rootViewController属性,如下所示:>
You can create an object of UINavigationController with your ViewController as per if/else condition and set the navigation controller as rootViewController property of window in AppDelegate as follows:
LoginViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;
这篇关于在Appdelegate的Storyboard中以编程方式为UINavigationcontroller设置rootViewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!