Sequenzia
Sequenzia

Reputation: 2381

Issue loading View Controller from UITabBarController

In the past my app has had only 1 main view controller (MainViewController) and a login view controller (LoginViewController) but now I am moving to a Tab Bar Controller.

Before I was able to do a simple check viewDidLoad of MainViewController for the existence of a username and password in the key chain. If a username and password was not present I used a segue to pop up a modal login view controller.

With the new setup of using a Tab Bar Controller I still only have 1 view controller (MainViewController) which is the root view controller (as of now) and I am trying to do the same thing where it pops up modal of the login screen.

Now when I call the segue in the viewDidLoad of MainViewController:

[self performSegueWithIdentifier:@"loadLoginView" sender:nil];

I am getting this error:

 Warning: Attempt to present <LoginViewController: 0x1757cd80> on <UITabBarController: 0x17571e50> whose view is not in the window hierarchy!

But if I associate a button to a method that loads the LoginViewController by way of a segue it works fine. I am doing that in the MainViewController like this:

-(void)loadLogin
{
    [self performSegueWithIdentifier:@"loadLoginView" sender:nil];
}

I can see from the error message that when I try to perform the segue from the viewDidLoad of MainViewController it's trying to load the LoginViewController from UITabBarController.

Why can I not load the LoginViewController from the viewDidLoad of MainViewController?

Any help with this would be great.

Thanks!

Upvotes: 0

Views: 422

Answers (1)

theMikeSwan
theMikeSwan

Reputation: 4729

It looks like -viewDidLoad is getting called before your view controller stack is added to the window. Two things you could try are:

Delaying until the next time through the run loop (this should give the view controllers time to get in place) [self performSelector:@selector(loadLogin) withObject:self afterDelay:0];. This method won't allow you to call a method with two arguments directly

You could use -presentViewController: animated: completion:. This will cause your login controller to slide in from the bottom.

Upvotes: 1

Related Questions