iphone - 在 Swift 中使用 shouldPerformSegueWithIdentifier( ) 方法

标签 iphone swift segue ios8 uistoryboardsegue

我正在尝试使用 swift 的 shouldPerformSegueWithIdentifier() 方法,但它接受 2 个参数。这些是(标识符:String!,发件人:AnyObject)

我的主要目标是在我的 Storyboard中按下登录按钮时执行代码,并根据是否提供了正确的用户名和密码,根据一系列检查返回 TRUE 或 FALSE。所以这是我的问题:

  1. 我应该使用什么作为标识符? Apple 的文档解释说标识符是标识触发的 segue 的字符串。所以假设我的 segue 的名称是 loginSegueProcess。我如何在分配给 UIView 的 ViewController 中使用它?问题是我在我的代码中声明了该方法,它要求我指定两个参数(标识符和发送者)。我如何提供论据?

  2. 这种方法真的能满足我的需求吗?我的意思是,如果它确实会在单击我的登录按钮时停止 segue 转换,并且取决于是否提供了正确的凭据,它将带您到下一个 View 或者它会显示,例如,一个 AlertView。

最后,我认为 performSegueWithIdentifier(args) 方法也会对我有所帮助。有人知道它们之间的区别吗?

提前致谢!

最佳答案

这不是你想做的吗?

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "LoginSuccessSegue" { // you define it in the storyboard (click on the segue, then Attributes' inspector > Identifier

        var segueShouldOccur = /** do whatever you need to set this var to true or false */

        if !segueShouldOccur {
            println("*** NOPE, segue wont occur")
            return false
        }
        else {
            println("*** YEP, segue will occur")
        }
    }

    // by default, transition
    return true
}

关于iphone - 在 Swift 中使用 shouldPerformSegueWithIdentifier( ) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25113602/

相关文章:

ios - 使用动态原型(prototype)单元在 segue 上将 CloudKit 记录从 TableViewController A 传递到 TableViewControllerB

iphone - 在 IOS 的 Cocoa Touch Tab Bar 应用程序前面添加登录屏幕

iphone - iPhone和Android横屏时如何限制网页缩放拉伸(stretch)?

swift - 在 Swift 中类型向下转型

swift - 尝试使用 AVFoundation AVAssetWriter 将 Metal 帧写入 Quicktime 文件时出现全黑帧

ios - 取决于函数返回值的条件转场

ios - 从表中传递选定的行以准备 segue Objective C

ios - 如何将水平滚动添加到 UIView?

ios - 为什么我的 iphone swift 应用程序分配并使用大量内存?

ios - 如何让我的形象隐藏在我的标签后面?