开发者

How to show alert before UIViewController is popped from UINavigationController

I have A UINavigationController which开发者_Go百科 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜