(Swift)在视图控制器之间传输数据

时间:2016-08-18 17:35:23

标签: ios swift

我正在尝试在视图控制器之间传输值。当我只是将我的按钮连接到另一个视图控制器时,如下所示:

enter image description here

我能够让它发挥作用。我压倒prepareforSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var DestinationViewController : ViewTwo = segue.destinationViewController as! ViewTwo
    DestinationViewController.scoretext = score.text!
}

一切正常但是当我以编程方式打开另一个控制器时,该值未被传输。这是我正在使用的代码...

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
self.presentViewController(vc, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:1)

使用instantiateViewControllerWithIdentifier时,不会调用prepareForSegue。您只需在实例化后分配scoretext。

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
vc.scoretext = score.text!
self.presentViewController(vc, animated: true, completion: nil)