本文介绍了在swift4中使用shouldPerformSegue(withIdentifier,sender)方法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做什么:
- 检查条件,如果条件为真,则执行segue为
正常。如果condition为false,则调用shouldPerformSegue方法,
返回false以取消segue。
我是如何尝试的这样做:
How I'm trying to do this:
func buttonTapped{
if(condition is true){
// Perform actions
// Perform segue as normal
} else{
shouldPerformSegue(withIdentifier "mySegueIdentifier", sender: self){
return false
}
}
}
我收到的错误:
- 电话中的额外参数。我删除了sender参数以尝试获取
这个工作,它为我摆脱了错误,但是当它不应该时,segue
仍然执行。
我在苹果文档中搜索了使用此方法的正确方法,但我仍然不确定如何操作。任何帮助将被感激地接受和高度赞赏。
I searched for the correct way to use this method in the apple documentation, but I am still unsure how to do so. Any help will be gratefully accepted and highly appreciated.
推荐答案
如果你想执行逻辑来决定是否应该执行segue 或不,你需要覆盖 shouldPerformSegue
。这将允许操作系统(1)正确启动事物,(2)让您能够决定是否完成segue。
If you want to perform logic to decide if a segue should be "performed" or not, you need to override shouldPerformSegue
. This will allow the OS to (1) kick off things properly and (2) for you to be able to decide if the segue gets done.
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if [criteria met to perform segue] {
return true
} else {
return false
}
}
这篇关于在swift4中使用shouldPerformSegue(withIdentifier,sender)方法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!