Search code examples
ioscrashuitabbarcontrollernszombie

Crash after a popToRootViewControllerAnimated


I have a crash when trying to switch to another VC. Here is the structure:

TabBarController [Home, Tab2, Result tab]

from these tabs, I can push a new viewController called addVC which contains a UITextField.

What I want to do, is to be able to follow this sequence: Tab2/Home (step1) => addVC (step2) => Result tab (step3)

To do this, I use this code in addVC :

[self.navigationController popToRootViewControllerAnimated:NO];
UITabBarController *tabbarController = ((UITabBarController*)appDelegate.window.rootViewController);
[tabbarController setSelectedIndex:2];

It works most of the time, but if the focus was in the textfield when I pop then push the result tab, the tab from the step1 is no longer accessible without a crash.

The crash log is:

[addVC respondsToSelector:]: message sent to deallocated instance 0x10977e60

NSZombie tells me that [UITextField canBecomeFirstResponder] has been called, causing the crash.

Since I use a storyboard, nothing except this is done programmatically. I guess my way to go to step3 is not the right one. I'm using ARC and I'm not releasing anything by myself.

Any ideas?

Thanks in advance.


Solution

  • For those who could have the same problem, the solution was to use this:

    [self.view endEditing:YES];
    

    Before popping anything, else the keyboard is still displayed and that's what causes the crash.