0

Guys in my app I have some code in the app delegate method application:didFinishLaunchingWithOptions: that determines if the initial View Controller should be the LoginViewController or the MainViewController.

If the LoginViewController is showed first and the user logs in successfully I show the MainViewController modally with this piece of code:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"MainViewController"];
vc.loginViewController = self;
[self presentViewController:vc animated:YES completion:nil];

What I want to do next, after the MainController is showed on the screen, is remove the LoginViewController from memory so in the viewWillApper:animated: method of the MainViewController I use this code to remove (or at least try to) the LoginViewController:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (self.loginViewController) {
        [self.loginViewController dismissViewControllerAnimated:NO completion:nil];
    }
}

Problem is that this code leads to strange behaviors like the MainViewController being removed from the screen and this error message showing up in the console.

Unbalanced calls to begin/end appearance transitions for <LoginViewController: 0xb06e350>

I also tried calling [self dismissViewControllerAnimated:NO completion:nil] in the completion block of the presentViewController:animated:completion method but still no luck, it didn't work.

What am I doing wrong? How can I remove from memory the underlying LoginViewController when the MainViewController is presented modally?

BigLex
  • 2,978
  • 5
  • 19
  • 27

2 Answers2

1

Don't present your main view controller if you want the login controller to go away, just make it the window's root view controller.

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"MainViewController"];
Self.window.rootViewController = VC;
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • This works but I would prefer the switching to be animated with some transition. Is there any way for doing that? – BigLex Feb 25 '14 at 16:56
  • Found a way to animate it at this link: http://stackoverflow.com/questions/7703806/rootviewcontroller-switch-transition-animation – BigLex Feb 25 '14 at 17:06
0

You can't dismissViewController after presenting another one on it or its presentingViewController. At here, you should dismiss LoginViewController first, then present MainViewController.

Otherwise, if you'd like pushViewController, you can call [self.navigationController setViewControllers: animated:] to remove LoginViewController.

If you think presentingViewController is just what you want, try something like this in application:didFinishLaunchingWithOptions:

if (self.loginViewController) { //Define loginViewController in appDelegate.h
    [self dismissViewControllerAnimated:NO completion:^{
        [self presentViewController:mainViewController animated:YES completion:nil];
    }];
}
else{
    [self presentViewController:mainViewController animated:YES completion:nil];
}
simalone
  • 2,768
  • 1
  • 15
  • 20