在this问题中,我需要将一个值从ViewController传递到另一个,第二个VC嵌入在导航控制器中。我尝试了这个问题的答案,但在控制台中打印的值总是为。无法弄清楚该怎么做。
在First VC中我有:
var dataToSend : String!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toSecondVCSegue" {
println("prepareForSegue occurred test value is: *\(dataToSend)*")
let destinationNavigationController = segue.destinationViewController as! UINavigationController
let targetController = destinationNavigationController.topViewController as! SecondViewController
targetController.receivedTest = dataToSend
}
}
@IBAction func toSecondVCButtonTapped(sender: UIButton) {
performSegueWithIdentifier("toSecondVCSegue", sender: nil)
dataToSend = "passed"
}
在第二个我有:
var receivedTest : String!
override func viewDidLoad() {
super.viewDidLoad()
println("in SecondViewController in viewDidLoad receivedTest is: *\(receivedTest)*")
}
override func viewWillAppear(animated: Bool) {
println("in SecondViewController in viewWillAppear receivedTest is: *\(receivedTest)*")
}
override func viewDidAppear(animated: Bool) {
println("in SecondViewController in viewDidAppear receivedTest is: *\(receivedTest)*")
}
答案 0 :(得分:1)
我认为,原因是您在调用dataToSend
之后将值设置为performSegueWithIdentifier
变量,因此它始终为零
尝试将代码更改为:
@IBAction func toSecondVCButtonTapped(sender: UIButton) {
dataToSend = "passed"
performSegueWithIdentifier("toSecondVCSegue", sender: nil)
}
这可能有所帮助!