Platano
Platano

Reputation: 11

In swift iOS 8 when a cell is clicked, how do I get it to open another view controller?

I'm fairly new to swift programming, and been trying for hours . Any help is well appreciated.

Upvotes: 0

Views: 58

Answers (4)

Rich
Rich

Reputation: 932

You need to create an instance of the View Controller that you want to open and then transition to that view controller - presumably, by 'pushing' that View Controller onto the navigation stack (navigation controller).

For example...

let nextViewController: MyViewController = MyViewController()
self.navigationController.pushViewController(nextViewController, animated:true)

Upvotes: 1

NRitH
NRitH

Reputation: 13893

To expand on @Epic Defeater's comment, if your table view controller and the destination view controller is in the same storyboard, control-drag from the table cell to the destination view controller. This is a segue, and it does what @Klevison Matias's code does, but with less involvement from you.

The segue needs a unique identifier, and in your table view controller, you must implement prepareForSegue:sender:.

Upvotes: 1

Epic Defeater
Epic Defeater

Reputation: 2147

Just add a segue from the cell to the view controller you want to connect it to.

Upvotes: 0

Klevison
Klevison

Reputation: 3484

Just override: didSelectRowAtIndexPath

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath path: NSIndexPath!) {
    let secondViewController = 
        self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController")
        as SecondViewController
    self.navigationController.pushViewController(secondViewController, animated: true)
}

Upvotes: 1

Related Questions