performSegue方法調用segue從一個視圖執行到另一個視圖。在segue實際發生之前,prepareForSegue方法被調用,如果你想在視圖之間傳遞數據,你可以在那裏做。
performSegue方法不接受你想發送的參數。它只是用於首先調用segue。您要發送的任何數據都將通過prepareForSegue完成。
下面是一個例子。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
performSegueWithIdentifier("test", sender: self)
//You can set the identifier in the storyboard, by clicking on the segue
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "test"{
var vc = segue.destinationViewController as! RandomViewController
vc.data = "Data you want to pass"
//Data has to be a variable name in your RandomViewController
}
}
讓我知道這是否有幫助!
感謝這個更新時,它救了我一點點回溯 - 我需要知道,如果prepareForSegue仍然會獨立檢查shouldPerformSegue的返回值的調用。 – MandisaW