0
votes

I have divided my project into two storyboards:

  • Main.storyboard - For authenticated user. Also this is the default storyboard.

  • Login.storyboard - For non-authenticated user.

App delegate file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(user is authenticated)
    {
        [self presentLoginScreen:YES];
        return YES;
    }

Login Screen in app delegate file:

-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = vc;
}

On logout, login Screen is presented. I defined this in app delegate file:

-(void)logOut{
   //clear data

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    //Show login Screen
    [self presentLoginScreen:NO];
}

From viewcontroller of MainStoryBoard(letsay mainstoryboardVc.m), logOut is called as:

-(void)didTouchLogOut{
    NSLog(@"GoodBye");
    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
    [appDelegateTemp logOut];
}

This is causing a lot of issue:

  • Firstly, when I log out, and move to login screen, I can still see mainstoryboardVc.m in the background which looks very clumsy.

  • I log out, re-log in and again trying to log out, I see following message:

Presenting view controllers on detached view controllers is discouraged

and login screen is not presented.

I tried many answers on the web on view controller presentations between two storyboards without keeping history, nothing seems to work...

2
So you did made things a lot more complicated without any gain? Why are you using two different storyboards?luk2302
Whats the point of using 2 storyboards?Teja Nandamuri
Coz the number of scenes were large, the two components are quite independent, and each storyboard was developed by different developers. Merging two will be the last option, but would be great if i don't have to take that last option..Ritesh Kumar Gupta

2 Answers

0
votes

To swap out UIViewControllers there are a few other methods that need to be called when transitioning the root view controller:

Objective-c:

- (void)setRootViewController:(UIViewController *) newRootViewController {

    UIViewController* currentViewController = self.window.rootViewController;
    if (newRootViewController != currentViewController) {
        [currentViewController willMoveToParentViewController:nil];
        [currentViewController.view removeFromSuperview];
        [currentViewController removeFromParentViewController];
        self.window.rootViewController = newRootViewController;
    }
}

Swift:

func setRootViewController(newRootViewController: UIViewController) {
    if let currentViewController = self.window?.rootViewController {
        if currentViewController != newRootViewController {
            currentViewController.willMoveToParentViewController(nil)
            currentViewController.view.removeFromSuperview()
            currentViewController.removeFromParentViewController()
            self.window?.rootViewController = currentViewController
        }
    }
}
0
votes

Two UIViewController objects are duplicated because you set self.window.rootViewController for both of them. Try that;

Login

-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}

Logout

-(void)logOut{
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
   UIViewController *vc =[storyboard instantiateInitialViewController];
   [self presentViewController:vc animated:YES completion:nil];
}