本文介绍了Swift prepareForSegue取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现登录屏幕,当点击登录时,它会执行segue登录。

I'm trying to implement a login screen, when login is clicked it does the segue "login".

我添加了一个prepareForSegue()覆盖试图如果登录失败,取消它,但如果失败,我没有看到任何取消segue的方法。

I added a prepareForSegue() override to try to cancel it if the login fails but I don't see any method to cancel the segue if there is a failure.

最好的方法是什么?

推荐答案

您应该覆盖 shouldPerformSegueWithIdentifier 并在登录失败时返回false:

You should override shouldPerformSegueWithIdentifier and return false if login failed:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    if let ident = identifier {
        if ident == "YourIdentifier" {
             if loginSuccess != true {
                  return false
             }
         }
     }
     return true
}

SWIFT 3更新
现在调用Swift 3方法 shouldPerformSegue

    override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool {
    if let ident = identifier {
        if ident == "YourIdentifier" {
            if loginSuccess != true {
                return false
            }
        }
    }
    return true
}

//扩展

如果以编程方式调用performSegueWithIdentifier,则不会调用此方法,但不需要调用此方法,您可以将其称为登录成功,否则忽略它:

If you programmatically call performSegueWithIdentifier this method will not be called but it's not need for that, you can call it just your login success, otherwise ignore it:

if loginSuccess {
    performSegueWithIdentifier("login", sender: nil)
}

这篇关于Swift prepareForSegue取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:36