Okan
Okan

Reputation: 1389

Using prepareForSegue for table view controller

I have a table view controller. I have to set a value inside this controller. But I guess above code works only for view controller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier! == "changeName" {
        if let destination = segue.destinationViewController as? NameSettingTableViewController {
            println("ok")
        }
    }
}

It doesn't print "ok" to debug. I think because NameSettingTableViewController is not a view controller. destination variable is returning nil. How can I fix this ?

Storyboard:http://prntscr.com/79846c

Upvotes: 0

Views: 314

Answers (1)

pbasdf
pbasdf

Reputation: 21536

You need to get the navigation controller first, then access its topViewController property to get a reference to your NameSettingTableViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier! == "changeName" {
        if let destination = segue.destinationViewController as? UINavigationController {
            let destVC = destination.topViewController as! NameSettingTableViewController
            // set whatever variables on destVC
            println("ok")
        }
    }
}

Upvotes: 1

Related Questions