hi all, I want to use popViewControllerAnimated instead of popToRootViewControllerAnimated to move to the root view.I do not want to move to root view directly.so is there any method to move to root view by popping all previous views?
views:
29answers:
1As far as I know, there isn't a direct method to do so.
You could of course build something like that out of a UINavigationControllerDelegate
that calls -[UINavigationController popViewControllerAnimated:]
as long as you didn't reach the root of the stack, but depending on how deep your stack grows and how often you want to have this behaviour, this might be very disrupting if not annoying to your users.
I.e. they can't do anything but sit and watch the views fly by while you pop one NavigationController after another...
Edit:
Sorry for the late response!
Said delegate-protocol defines a method named - navigationController:didShowViewController:animated:
. If you implement that protocol in a class with a property of—say—shouldPopToRoot
, you could set that to YES in the action where you trigger the popping.
Said method could then be along the lines of
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ( self.shouldPopToRoot )
{
// Since this method gets called WHENEVER you pop a view-controller, you need
// to ensure that you stop popping if you've just shown the root-view-controller
if ( [navigationController.viewControllers itemAtIndex:0] == viewController )
self.shouldPopToRoot = NO;
else
[navigationController popViewControllerAnimated:YES];
}
}