Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

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

I can open the app using a URL also

- (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 rootViewController for the UINavigationController based on the value that is retrieved .Can anyone please help me out?

like image 675
Aswathy Bose Avatar asked Dec 26 '13 08:12

Aswathy Bose


People also ask

How do I change the root view controller in storyboard?

1) Remove Storyboards from app. plist file, 2) Uncheck option "isInitialViewController" from Storyboards which is checked in case of Tab Bar controller because its a root ViewController , 3) Add this code in appDelegate. m file.

How do I change the root view controller in swift 5?

plist file → Application Scene Manifest property → Scene Configuration → item 0 and get rid of the property Storyboard Name by clicking the icon that has a minus in the circle next to it. ► Run the app and you will see the black screen and let's change that by setting a new root view controller.


2 Answers

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;
like image 180
dispatchMain Avatar answered Oct 11 '22 00:10

dispatchMain


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 link for implementation with navigation controller

like image 33
Prince Agrawal Avatar answered Oct 11 '22 01:10

Prince Agrawal