向模式推送 segue 添加条件



我使用导航控制器和模态显示 segue。 如果 [条件] 不为 true,我想阻止我的第二个视图控制器的后退按钮,例如,当用户按下后退按钮时添加警报"在 [条件] 之前无法返回"。

不知道是否有可能,如果有人知道如何解决它!

谢谢

您需要为后退栏按钮项添加自定义操作。

@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(_:)))

很抱歉语法不正确。

需要后退按钮图像"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)
}

最新更新