简体   繁体   中英

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.

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)
}

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: .

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)

只需将单元格中的segue添加到要与其连接的视图控制器即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM