传递数据时有条件的Segue?

Conditional Segue while passing data?

我试图仅在满足我的条件时从 viewcontroller 到 2ndviewcontroller 进行转场。我已将 segue 从 viewcontroller 中的按钮连接到第二个 viewcontroller。到目前为止我有:

@IBAction func switchView(_ sender: AnyObject) {
        // if a and b's textfields aren't empty
        if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) {
            a1 = a.text!;
            b1 = b.text!;
        }else{
            // do something
        }

    }

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if(identifier == "2ndviewcontroller") {
        if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!)  {
            return true
        }
    }
    return false
}

有了这个,我只能在 a 和 b 的文本字段不为空时进行转场。这正是我想要的,但我也想将数据从 viewcontroller 传递到 2ndviewcontroller。

func prepForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "2ndviewcontroller" {
         let 2ndviewcontroller = segue.destination as! 2ndviewcontroller
            2ndviewcontroller.c = a

        }
    }
}

我使用上面的代码将数据从 viewcontroller 传递到 2ndviewcontroller。问题是我不知道如何将它们组合起来传递数据,并且只在满足条件时才进行转场。当我同时拥有这两个函数时,bool 函数会正确执行,但 prepForSegue 不会传递数据。当我注释掉 bool 函数时,prepForSegue 传递了数据,但我无法应用进行转场的条件。

编辑:使用下面评论中提供的 prepareForSegue 方法修复。

如评论中所述,方法名称应为 prepareForSegue,而不是 prepForSegue