1

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)*")

    }
Community
  • 1
  • 1
biggreentree
  • 1,633
  • 3
  • 20
  • 35
  • after posting question I realized, wrong order in the button func, perform segue should be the last statement :( now it works!, maybe you can help me another way, do you think I should call a super. in the viewWillAppear viewDidAppear? – biggreentree Sep 07 '15 at 11:18
  • Yes, super must be there. There are rare situations when you need to avoid using super in these methods. It will be like super.viewWillAppear(animated) – AlexZd Sep 07 '15 at 11:21
  • but if I write override func viewWillAppear(animated: Bool) { super.viewDidAppear(animated: Bool) I got an error :( – biggreentree Sep 07 '15 at 11:23
  • Inside viewWillAppear: you need to call super.viewWillAppear, inside viewDidAppear: need to call super.viewDidAppear: – AlexZd Sep 07 '15 at 11:24
  • so, this is wrong? `override func viewWillAppear(animated: Bool) { super.viewWillAppear` – biggreentree Sep 07 '15 at 11:26
  • Provide error. It should be like `override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) }` – AlexZd Sep 07 '15 at 11:30
  • I have answer this question in objectiveC you can easily convert it into swift have a look on the concept http://stackoverflow.com/questions/32264264/how-to-not-call-viewdidload-in-uinavigationcontroller/32266115#32266115 – baydi Sep 07 '15 at 11:30

1 Answers1

1

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!

iRiziya
  • 3,235
  • 1
  • 22
  • 36