在Appdelegate的Storyboard中以编程方式为UINavigationcontroller设置rootViewcontroller

时间:2013-12-26 08:26:26

标签: objective-c uinavigationcontroller storyboard nsuserdefaults appdelegate

我在NSUserdefaults中有一个值。我使用的是storyboard,它嵌入在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;
}

如何根据检索到的值为rootViewController设置UINavigationController。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:30)

您可以根据if / else条件使用ViewController创建UINavigationController的对象,并将导航控制器设置为AppDelegate中窗口的rootViewController属性,如下所示:

LoginViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;

答案 1 :(得分:2)

这是我在我的代码中使用的

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // Get user preference
 NSString * wxyz=[defaults stringForKey:@"wxyz"];
    //Get value at wxyz field

if ([self isInValidwxyz:wxyz]) {
    //check if wxyz is invalid
    //If wxyz is invalid, write custom code
}
else{
    //if valid, 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryBoardiPhone" bundle:nil];
        self.window.rootViewController = [storyboard instantiateInitialViewController];
    }
    else{

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryBoardiPad" bundle:nil];
        self.window.rootViewController = [storyboard instantiateInitialViewController];;
    }
    [self.window makeKeyAndVisible];
}

然后,使用导航控制器

执行此link以实现