2016-08-18 60 views
0

我正在嘗試在我的視圖控制器之間傳輸一個值。當我只是我的按鈕連接到其他視圖控制器,這樣的:(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

當您使用instantiateViewControllerWithIdentifierprepareForSegue不叫。您只需在實例化後指定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) 
+0

謝謝,這樣做! – Ash

相關問題