Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigation controller Push and Pop

I have to pop to HomeScreenViewController from current view controller after clicking on custom back button, which is added on main window as a light box. I used following code :

HomeScreenViewController *homeController = [[HomeScreenViewController alloc] 
initWithNibName:@"HomeScreenViewController" bundle:nil];
    [self.navigationController popToViewController:homeController animated:YES];
    [homeController release];

I got crashing with exception : Tried to pop to a view controller that doesn't exist.

How can it be implemented? What changes are required for implementing it?

like image 469
Lalit Paliwal Avatar asked Jan 19 '23 03:01

Lalit Paliwal


1 Answers

Clearly you are creating a new instance of HomeScreenViewController which doesn't exist on the navigation stack. You will have to get the existing instance and use it as an argument for popToViewController:animated: method. You can do it by getting the view controller from the viewControllers array which is a property on UINavigationController. They are indexed in order so if the view controller is at index 1 then get the view controller using

UIViewController * viewController = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:viewController animated:YES];

If you want to go back to root view controller, use popToRootViewControllerAnimated: instead.

like image 193
Deepak Danduprolu Avatar answered Jan 30 '23 09:01

Deepak Danduprolu