ios - 调用 performSeguewithIdentifier 不会调用 shouldperformseguewithIdentifier

标签 ios swift segue

我有两个 View Controller 。在 View Controller 1 上,我有以下内容:

  • 将我带到 viewcontroller2 的 segue - 这个 segue 被命名为“showme”并附加到 viewcontroller
  • UIButton 的 IBAction

在我的代码中,我有以下按钮按下操作

@IBAction func buttonPress(sender: AnyObject) {
    println("button pressed")
        performSegueWithIdentifier("showme", sender: self)
}

我还有以下方法:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    println("Should performing....")
    return true
}   

出于某种原因,从未调用 shouldPerformSegueWithIdentifier 函数。但是,如果我直接在 UIButton 上将 segue 添加到 ViewController2。

我已经确认在我的按钮操作中调用方向是有效的(见下文),但这不是我所理解的它的工作方式。 prepareforSegue 也是如此..

@IBAction func buttonPress(sender: AnyObject) {
    println("button pressed")
    if (shouldPerformSegueWithIdentifier("showme", sender: self)){
        performSegueWithIdentifier("showme", sender: self)}
} 

最佳答案

这种行为是完全自然的,原因如下:

1) shouldPerformSegueWithIdentifier 用于确保在Storyboards 中设置的 segue 应该被触发,所以它只在这种情况下被调用Storyboard Segues 并让您有机会实际执行 segue。

2) 当您自己调用 performSegueWithIdentifier 时,shouldPerformSegueWithIdentifier 不会被调用,因为可以假设您知道自己在做什么。调用 performSegueWithIdentifier 是没有意义的,但随后从 shouldPerformSegueWithIdentifier 返回一个 NO

关于ios - 调用 performSeguewithIdentifier 不会调用 shouldperformseguewithIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946566/

相关文章:

iphone - 根据 iOS 版本禁用委托(delegate)方法

Swift-Firebase 身份验证登录功能在呈现的 segue 中,如何使用 shouldPerformSegue 来阻止 segue 执行?

ios - Swift float 减法的精度损失

ios - 在代码中混合 Storyboard和约束

swift - 如何修改 Swift 4 中的 fileManager url 以指向另一个目录

swift - 如何在 Swift 中的 GUI 中只允许一个窗口

ios - applicationDidBecomeActive 保持登录状态,类似于 instagram

ios - 我如何使用我的 Action 并以编程方式转换到 Storyboard 中定义的 View Controller ?

ios - 回到已经被转入并且已经加载的 viewController?没有导航 Controller

ios - 迁移 Swift 3.0 - 带有 RNCryptor 和其他库的 Podfile