我在解决有关segue
的问题时遇到了困难。场景应该是当用户点击backbutton
时,它将提示一个警报,询问您您确定要离开此页面吗? Leave
或Stay
是选项。如果用户点击Leave
响应,它将返回到DashBoard
,如果用户点击Stay
,它将仅保留其原始ViewController
。问题是segue
会自动执行而不会检查backbutton
内部的条件。我是新手,遇到的所有问题对我来说都是新的。如何正确构造代码以满足backbutton
的要求。在执行顺序之前,按钮将如何首先执行条件?希望您能够帮助我。以下是我的代码供您参考。在这里待了将近1周。谢谢。
执行Segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDashBoard" {
if let dashBoardVC = segue.destination as? DashBoardViewController {
dashBoardVC.validPincode = validPincode
dashBoardVC.participants = participants
dashBoardVC.event = event
}
}
}
后退按钮
@IBAction func backbutton(_ sender: UIButton) {
let alert = SCLAlertView(appearance: confirmationAppearance)
_ = alert.addButton("Leave", action: {
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "showDashBoard", sender: sender)
})
_ = alert.addButton("Stay", action: { })
_ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")
}
尝试使用shouldPerformSegue,但我不知道如何开始
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "showDashBoard" {
var segueShouldOccur: Bool!
if segueShouldOccur == false {
print("nope, user wants to stay")
return false
}
else {
self.performSegue(withIdentifier: "showDashBoard", sender: sender)
print("Yep, the user wants to leave")
}
}
return true
}
答案 0 :(得分:0)
为了像我这样的人的利益,新手请使用swift。我解决了自己的问题。我省略了back button
和DashBoard
之间的连接。我从ParticipantsViewController
到DashBoardViewController
转移了segue::)
@IBAction func backbutton(_ sender: UIButton) {
let alert = SCLAlertView(appearance: confirmationAppearance)
_ = alert.addButton("Leave", action: {
self.dismiss(animated: true, completion: {
self.performSegue(withIdentifier: "showDashBoard", sender: sender)
})
})
_ = alert.addButton("Stay", action: { })
_ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")
}