Reputation: 1923
As in this question, I need to pass a value from a ViewController to another one, the second VC is embedded in navigation controller. I tried the answer to that question but value printed in console is always nil. Can't figure out what to do.
In First VC I have:
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"
}
in the second I have:
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)*")
}
Upvotes: 1
Views: 235
Reputation: 3245
I think, the reason is you set value to dataToSend
variable after calling performSegueWithIdentifier
and so it stays always nil
Try changing your code as :
@IBAction func toSecondVCButtonTapped(sender: UIButton) {
dataToSend = "passed"
performSegueWithIdentifier("toSecondVCSegue", sender: nil)
}
This may help!
Upvotes: 1