简体   繁体   中英

How to show alert before UIViewController is popped from UINavigationController

I have A UINavigationController which is managing multiple UIViewControllers. When at the top of the view controller hierachy, and when back is pressed, I want to show a UIAlertView to ask the user if they are sure that they want to go back. What is the best way to check if the view is popped?

I think there is no specific message for the back button.
But you can try to subclass UINavigationController and then override the method popViewControllerAnimated: . (I haven't tried it.)

Another option is to create a custom back button of the type UIBarButtonItem and add a target and action for this button.

This can be done by subclassing UINavigationController and overriding (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { }

overriding popViewControllerAnimated: is too late to cancel a pop.

I think that the only way that works is taking a screenshot of the view, cutting out the button, and then adding a uibutton to you navigationController bar. Then you set the image that you have cut out to be the buttons image, after that you create an action with just a uialertview. Set your class as Uialertviewdelegate, and pop the viewcontroller when the user presses the ok button using

- (void)alertView:(UIAlertView *)actionSheet     clickedButtonAtIndex: (NSInteger)buttonIndex {
   if (buttonIndex == 0)
    {
       //pop viewcontroller
    }
  }

What about using the UINavigationControllerDelegate-Methods like

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

and check if the controller which should get pushed is of that kind that it needs an warning. The other way around would also work!

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