简体   繁体   中英

Custom logic for back button press in UINavigationController

A more detailed explanation of my app layout: clicking on any of the rows of the table view will take the user to the next screen with a back button pointing back to home and a segmented control on the top. A logic in the code determines which of the segments of the segmented control will be preselected. The user can click on any of the indexes of the segmented control to change the content displayed on the screen.

The way I have implemented the above layout is through a navigation controller. The content of each of the pages corresponding to “first, second & third" of the segmented control are all separate view controllers. The reason I have it this way is because each of these pages have significant functionality and controls for the user to interact with. Keeping them each as a separate view controller helps software code organization and data integrity. The home screen is at index zero of the stack of navigation controllers, view controller corresponding to first at index one of the navigation controller and so on. Let's say the user is currently on the second screen with “first”selected in the segmented control. If the user now clicks on "third", two view controllers are pushed onto the stack and vice versa for popping controllers out of the navigation stack.

Two questions: • any comments on the way I have implemented? Are there suggestions for any better implementations? One specific implementation that I did consider is the possibility of having one view controller with three separate views (one each for first, second & third)? Any comments on this approach?

•   I seem to have an extremely hard time controlling the behavior of the “back button”of the navigation controller. When the user has selected “second”in the segmented control, I would still like to have the back button saying “Home” instead of “first” which is the default behavior of the navigation controller. I have figured out how I can customize the text of the back button. However, I can't seem to figure out how to customize the behavior of the button. What I mean by that is, when the user is on "third”, and clicks on the “home button”I'd like to pop three view controllers and land the user on the home screen.

On SO, I saw and tried various techniques with no success: approach 1: viewwillDisappear(): determine if this function is being called as part of a back button press and implement the logic of popping additional view controllers beyond the standard one view controller pop. For a moment, this logic does indeed pop back to the home page however it immediately crashes with the following message which I don't seem to understand:

approach 2: didPopItem(): I put the following code in this function

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {

   NSLog(@"%s",__FUNCTION__);
      [[self navigationController] popViewControllerAnimated:YES];
    //ViewControllerAnimated:YES];
   NSLog(@"navcount%d",self.navigationController.viewControllers.count);
   if (self.navigationController.viewControllers.count > 1) {
         [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
   }

}

any comments on the above will be much appreciated! Thanks in advance for your help.

Since the three view controllers are really equal to each other in your view hierarchy, I would suggest replacing the top view controller instead of pushing multiple view controllers when you switch between your segments so that you can go "Back" from any of the three view controllers and you will end up where you want to be.

Something like this should work for you:

- (void)replaceTopViewControllerWith:(UIViewController *)vc {
    NSMutableArray *vcs = [[self.navigationController viewControllers] mutableCopy];
    [vcs removeLastObject];
    [vcs addObject:vc];
    [self.navigationController setViewControllers:vcs animated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM