简体   繁体   English

Swift - 在点击 TableViewCell 时打开 ViewController

[英]Swift - Open ViewController on Tapping a TableViewCell

I have a tableView with identical cells.我有一个具有相同单元格的 tableView。 After selecting one of them I would like to open another ViewController.选择其中之一后,我想打开另一个 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.但它没有打开另一个 ViewController。 Can You give me a hint?你能给我一个提示吗?

Most probably you just don't have the navigationController there - use rather present to show the viewController:很可能你只是没有navigationController控制器 - 使用而present来显示视图控制器:

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 .或者,如果您想要推送 viewController,请确保将tableViewController嵌入到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.这意味着您已经从初始视图控制器为视图控制器设置了导航堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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