我使用导航控制器和模态显示segues。 如果[条件]不成立,我想阻止第二个视图控制器的后退按钮,例如,当用户按下后退按钮时,添加警告“你不能回到[条件]”。
如果有人知道如何解决它,不知道是否可能!
Thanksss
答案 0 :(得分:2)
您需要为后栏按钮项添加自定义操作。
@IBAction func backPressed(_ sender: UIBarButtonItem) {
if !myCondition {
let alert = UIAlertViewController(title: "Alert", message: "Please fulfill the condition")
let action = UIAlertAction(title: "OK", .default)
self.present(alert, animated: true)
return
}
navigationController?.popViewController(animated: true)
}
或者您可以使用代码中的常规目标操作。
myBarButton.addTarget(self, selector: #selector(backButtonPressed(_:)))
抱歉语法不正确。
答案 1 :(得分:1)
需要后退按钮图片“ic-menu-back-primary”
override func viewDidLoad() {
super.viewDidLoad()
// Nav Back Button
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: #imageLiteral(resourceName: "ic-menu-back-primary"), style: .plain, target: self, action: #selector(back(_:)))
navigationItem.leftBarButtonItem = backButton
}
@IBAction func back(_ sender: Any?) {
let alert = UIAlertController(title: nil, message: "You can't go back until [condition]", preferredStyle: .alert)
let resume = UIAlertAction(title: "Cancel", style: .cancel)
let cancel = UIAlertAction(title: "Exit", style: .default) { (action) in
self.navigationController?.popViewController(animated: true)
}
alert.addAction(resume)
alert.addAction(cancel)
present(alert, animated: true)
}