简体   繁体   中英

Swift - Open ViewController on Tapping a TableViewCell

I have a tableView with identical cells. After selecting one of them I would like to open another ViewController. I tried this code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {

    let destination = EditViewController()
    navigationController?.pushViewController(destination, animated: true)

}

but it didn't open another ViewController. Can You give me a hint?

Most probably you just don't have the navigationController there - use rather present to show the viewController:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    let destination = EditViewController()
    self.present(destination, animated: true, completion: nil)
}

Or if you want the viewController to be pushed, make sure that you embed the tableViewController into a UINavigationController .

Just go inside you storyboard, Select you initiate view controller, Then click on Editor > Embedded In and select Navigation controller, Then Your code will be work. This mean you have set the navigation stack for your view controllers from initial view controller.

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