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

时间:2015-01-02 18:53:58

标签: ios swift xcode6

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

my storyboard

我写了这段代码

@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
    }



}

此代码无效

1 个答案:

答案 0 :(得分:1)

从初学者的提交功能中取出prepareForSegue()方法。在那之后查找“performSegueWithIdentifier”,你会看到这一切是如何工作的。

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

Sending data with Segue with Swift