我正在使用此代码转到下一个屏幕
var nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("MainVC") as ViewController
self.navigationController?.pushViewController(nextVC, animated: true)<br>
现在我想将布尔值传递到下一个屏幕我该如何通过?
答案 0 :(得分:1)
只需设置变量?您已经拥有viewController实例。
var nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("MainVC") as ViewController
nextVC.myFancyBoolVariable = true
self.navigationController?.pushViewController(nextVC, animated: true)
答案 1 :(得分:0)
查看prepareForSegue(_:sender:)
记录的here。
答案 2 :(得分:0)
将数据从ViewController1发送到ViewController2:
ViewController1:
// define var
var myboolvar: Bool = true
var anyOtherVar: String = "test"
// prepare
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "editCardSet"){ // name your segue in storyboard
var upcoming: ViewController2 = segue.destinationViewController as ViewController2 // set the target ViewController
// transport any data, as many as you need
// make sure these vars are set in the seocnd ViewController, otherwise you will get an error here
upcoming.receiveMyBool = self.myboolvar
upcoming.receiveMyVar = self.anyOtherVar
}
}
在ViewController2中:
class ViewController2: UITableViewController {
// define the vars you are sending from the previous controller
var receiveMyBool: Bool!
var receiveMyVar: String!