如何在 Swift 中的视图之间传递数据

how to pass data between view in Swift

我有两个视图控制器,我想在按下确定按钮时将数据从第一个视图传递到第二个视图,并在按下取消时移动到第二个视图而不传递任何数据

我写了这段代码

@IBAction func submitL() {
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        var third = segue.destinationViewController as ViewController
        third.lastName = theLastName.text
        third.name = theName.text
    }



}

此代码无效

初学者请将 prepareForSegue() 方法从提交函数中移除。在那之后查找 "performSegueWithIdentifier",您将看到这一切是如何工作的。

self.performSegueWithIdentifier("nameOfYourSegue", sender: self)

Sending data with Segue with Swift