iOS Warning: Attempt to present AViewController on BViewController whose view is not in the window hierarchy!

Posted: December 3, 2014 in iOS

As the message described, you are trying to launch a view controller on top of the other one which has been removed from the window hierarchy.

The reason can be different. For example, one said the second view controller was pushed modally in viewDidLoad where the first view controller hasn’t added to window hierarchy.

In my case, it’s because of the unwinded segue triggered by UITextField.

My login view controller has two text fields (name and password) and a login button. The screen is designed in storyboard and when user click the login button, it will perform the login segue.

But I always see this warning: whose view is not in the window hierarchy when I set the segue as push modally. When i changed it to Show, the warning is gone, but the home view controller flashed twice.

So i suspected that it’s because the segue was performed twice.

To confirm the reason, you can override the function prepareForSegue and set the break point inside. I found the break point was hit twice and in the second time, the sender is UITextField.

Because I only perform segue programatically, the fix is very simple. Just override the shouldPerformSegueWithIdentifier and return NO because the programmatically performed segue won’t trigger this method.

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    return NO;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s