【问题标题】:How to show alert before UIViewController is popped from UINavigationController如何在 UIViewController 从 UINavigationController 弹出之前显示警报
【发布时间】:2011-08-03 16:59:17
【问题描述】:

我有一个 UINavigationController 管理多个 UIViewControllers。当在视图控制器层次结构的顶部,当按下返回时,我想显示一个 UIAlertView 来询问用户是否确定要返回。检查视图是否弹出的最佳方法是什么?

【问题讨论】:

    标签: iphone objective-c xcode uinavigationcontroller


    【解决方案1】:

    我认为后退按钮没有具体的消息。
    但是您可以尝试继承 UINavigationController,然后重写方法 popViewControllerAnimated:。 (我没试过。)

    另一种选择是创建 UIBarButtonItem 类型的自定义后退按钮,并为该按钮添加目标和操作。

    【讨论】:

      【解决方案2】:

      这可以通过继承 UINavigationController 和覆盖来完成 (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item { }

      覆盖 popViewControllerAnimated: 为时已晚,无法取消弹出。

      【讨论】:

        【解决方案3】:

        我认为唯一可行的方法是截取视图的屏幕截图,剪掉按钮,然后将 uibutton 添加到您的导航控制器栏。然后,您将已剪切的图像设置为按钮图像,然后创建一个仅使用 uialertview 的操作。将您的类设置为 Uialertviewdelegate,并在用户使用

        按下确定按钮时弹出视图控制器
        - (void)alertView:(UIAlertView *)actionSheet     clickedButtonAtIndex: (NSInteger)buttonIndex {
           if (buttonIndex == 0)
            {
               //pop viewcontroller
            }
          }
        

        【讨论】:

          【解决方案4】:

          使用 UINavigationControllerDelegate-Methods 怎么样

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

          并检查应该被推送的控制器是否属于需要警告的那种。反过来也行!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-18
            • 2017-12-06
            相关资源
            最近更新 更多