(Swift)在视图控制器之间传输数据
问题描述:
我正在尝试在我的视图控制器之间传输一个值。当我只是我的按钮连接到其他视图控制器,这样的:(Swift)在视图控制器之间传输数据
我能够使它发挥作用。我压倒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)
答
当您使用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)
谢谢,这样做! – Ash