Search code examples
iphoneiosipadios6

Doesn't pop to Parent view from the Child view controller


I have 3 view controllers "Root", "Parent" & "Child". Now i am pushing into Child from a method of Parent. Now when i want to pop to parent from the Child view through the following code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
Parent *svc = [storyboard instantiateViewControllerWithIdentifier:@"Parent"];

[self.navigationController popToViewController:svc animated:YES];

This shows the error:

'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

When i write the following code instead, It pop to a blank screen! :

[self.navigationController popViewControllerAnimated:YES];

And when I write the following code, It pop to Root. :

[self.navigationController popToRootViewControllerAnimated:YES];

But I want to pop to Parent view exactly. How can I do so?

Thanks in advance.

Push example from class Parent:

-(void)Custom{

if([info isEqualToString:@"message"]){

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];

Child *cd = [storyboard instantiateViewControllerWithIdentifier:@"Child"];

[self.navigationController pushViewController:cd animated:YES];
   }
}

Pop example from Child:

-(void)viewDidLoad{

 [super viewDidLoad];

 [self sendMessage]

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
Parent *svc = [storyboard instantiateViewControllerWithIdentifier:@"Parent"];

[self.navigationController popToViewController:svc animated:YES];

}

Solution

  • Yo can't pop to the Child view controller because is not added to the navigation controller stack. (You make a new instance of Child when you call [storyboard instantiateViewControllerWithIdentifier:@"Child"];)

    If you push Parent and after that you push the child form the Parent, and if you call from the Child [self.navigationController popViewControllerAnimated:YES] it should work.