【问题标题】:Add Alert for Nav Bar Back Action Before Go to View Controller在转到视图控制器之前为导航栏返回操作添加警报
【发布时间】:2019-01-07 15:51:07
【问题描述】:

所以我有一个添加条目 vc,对于后退按钮,我希望弹出一个警报,询问用户是否要继续,因为他们的信息将会丢失。我一直在阅读并发现使用此功能,但它不能按我的意愿工作。当视图加载并返回到主 vc 后,会出现警报。这是代码和一些图片。谢谢。

override func willMove(toParent parent: UIViewController?) {
    let alertController = UIAlertController(title: "Are You Sure?", message: "If You Proceed, All Data On This Page Will Be Lost", preferredStyle: .alert)
    let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
    alertController.addAction(action)
    self.present(alertController, animated: true)
}

【问题讨论】:

  • 您需要创建一个自定义后退按钮,该按钮以显示警报的操作为目标。然后只需检查用户是否按下 ok,弹出 viewController。如果用户按下取消,则关闭警报。
  • vc 自动有一个后退按钮,因为它嵌入在导航控制器中。 @GaloTorresSevilla
  • 你可以隐藏它并使用你自定义的。

标签: swift navigation tableview back


【解决方案1】:

您必须像这样在viewDidload 中添加自定义后退按钮

   override func viewDidLoad() {
            super.viewDidLoad()
            let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.backAction(sender:)))
            self.navigationItem.leftBarButtonItem = newBackButton

        }

这是后退按钮的操作方法,因此您可以在此处添加警报并添加任何您想要的操作:

@objc func backAction(sender: UIBarButtonItem) {

    let alertController = UIAlertController(title: "Are You Sure?", message: "If You Proceed, All Data On This Page Will Be Lost", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "Ok", style: .default) { (result : UIAlertAction) -> Void in
        self.navigationController?.popViewController(animated: true)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(okAction)
    self.present(alertController, animated: true)
}

【讨论】:

  • 我这样做了,但它得到了 .摆脱导航栏默认后退按钮上的后退箭头。有什么方法可以在标题中保留后退箭头并返回文本?
  • 可以自定义添加返回箭头图标
【解决方案2】:
override func viewDidLoad {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
        self.navigationItem.leftBarButtonItem = newBackButton
    }

    func back(sender: UIBarButtonItem) {
        let alertController = UIAlertController(title: "Are You Sure?", message: "If You Proceed, All Data On This Page Will Be Lost", preferredStyle: .alert)
        let stayAction = UIAlertAction(title: "Stay", style: UIAlertAction.Style.default) { (result : UIAlertAction) -> Void in
          print("Stay")
        }
        let leaveAction = UIAlertAction(title: "GO Back", style: UIAlertAction.Style.default) { (result : UIAlertAction) -> Void in
           self.navigationController?.popViewController(animated: true)
        }
        self.present(alertController, animated: true)
    }

【讨论】:

    猜你喜欢
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2014-07-14
    • 2017-01-07
    相关资源
    最近更新 更多