0

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!

4

1 回答 1

1

看起来-viewDidLoad在您的视图控制器堆栈添加到窗口之前被调用。您可以尝试的两件事是:

延迟到下一次运行循环(这应该让视图控制器有时间到位)[self performSelector:@selector(loadLogin) withObject:self afterDelay:0];。此方法不允许您直接调用带有两个参数的方法

你可以使用-presentViewController: animated: completion:. 这将导致您的登录控制器从底部滑入。

于 2013-07-03T20:43:12.653 回答