,可以通过以下步骤实现:
viewDidLoad
方法中添加以下代码,以便在导航栏返回按钮被点击时触发警报:override func viewDidLoad() {
super.viewDidLoad()
// 添加导航栏返回按钮点击事件
navigationItem.leftBarButtonItem?.action = #selector(showAlert)
}
showAlert
的方法,用于显示警报:@objc func showAlert() {
let alertController = UIAlertController(title: "返回操作", message: "确定要返回吗?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
let confirmAction = UIAlertAction(title: "确定", style: .default) { (_) in
// 在这里处理返回操作
self.navigationController?.popViewController(animated: true)
}
alertController.addAction(confirmAction)
present(alertController, animated: true, completion: nil)
}
prepare(for:sender:)
方法中设置返回按钮的标题,或者在目标视图控制器的viewWillAppear(_:)
方法中设置返回按钮的标题。这样,当用户点击导航栏返回按钮时,将会显示一个警报框,询问用户是否确定要返回。如果用户点击了确定按钮,将执行返回操作,否则取消返回。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云