JSL JSL - 7 months ago 16
Swift Question

Pass data to another view controller in navigation controller

I have two view controllers (fitstViewController.swift and secondViewController.swift) in navigation controler.

My idea is that
In firstViewController there are a button and a textFIeld. When I click the button, value in the textField is passed to secondViewController.

However it makes an error "Thread 1: signal SIGABRT" in the first controller.

Here is the code in the first controller..

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! UINavigationController

let detailController = destination.topViewController as! secondViewController

detailController.stockSymbol = textField.text
}


I added just one line in the second controller..

var stockSymbol:String!


How can it be solved?

Thank you in advance!

Answer

Replace your old code by:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destination = segue.destinationViewController as! SecondViewController
    destination.stockSymbol = textField.text
}