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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20781591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:10:19  来源:igfitidea点击:

Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

objective-cuinavigationcontrollerstoryboardnsuserdefaultsappdelegate

提问by Aswathy Bose

I have a value in NSUserdefaults. I am using storyboard, it is embedded in UINavigationController.

我在NSUserdefaults. 我正在使用storyboard,它嵌入在UINavigationController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     if([[NSUserDefaults standardUserDefaults]objectForKey:@"isLoggedIn"]){
         //show home page here
        }else{
           // show login view
        }
}

I can open the app using a URLalso

我可以使用打开应用程序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;
}

How can I set the rootViewControllerfor the UINavigationControllerbased on the value that is retrieved .Can anyone please help me out?

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

回答by dispatchMain

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:

您可以根据 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;

回答by Prince Agrawal

This is what i used in my code

这是我在代码中使用的

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];
}

and then, go through this linkfor implementation with navigation controller

然后,通过此链接使用导航控制器实现