Backbutton that goes back two or more Controllers
I know that overriding the back button functionality is not considered a good user design. However I have process, at which at some point going back would'nt make any sense. Instead I would like the user to go back two or more controller
So within certain ViewControllers, clicking the back-button should trigger the pop of sever开发者_高级运维al ViewControllers, not just the one in front. I tried around with subclassing the NavigationController and overriding the popViewController-Method:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if([[self.viewControllers lastObject] class] == [MyCOntroller class]){
[super popViewControllerAnimated:NO]; // pop once
return [super popViewControllerAnimated:animated]; // pop twice
} else {
return [super popViewControllerAnimated:animated];
}
}
However I get problems, where the NavigationTopBar is not in sync anymore withe the ViewController being in front. Anybody ran into the same issues?
Did u try using
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
Maybe u can have custom back buttons for such view controllers and then try
- (IBAction)backButtonPressed
{
[yourNavigationcontroller popToViewController:viewController animated:YES];
}
You should add left bar button.
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButton_clicked)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release];
And
-(void)backButton_clicked {
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
//or pop to special view controller
//[self.navigationController popToViewController:specVC animated:YES];
}
Another way is to delete the viewcontrollers you want to skip from the Navigation stack. In example below, you go back 2 view controllers:
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:[allViewControllers count]-2]; // 2 means last but one
self.navigationController.viewControllers = allViewControllers;
精彩评论