繁体   English   中英

Swift - 在点击 TableViewCell 时打开 ViewController

[英]Swift - Open ViewController on Tapping a TableViewCell

我有一个具有相同单元格的 tableView。 选择其中之一后,我想打开另一个 ViewController。 我试过这个代码:

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

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

}

但它没有打开另一个 ViewController。 你能给我一个提示吗?

很可能你只是没有navigationController控制器 - 使用而present来显示视图控制器:

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

或者,如果您想要推送 viewController,请确保将tableViewController嵌入到UINavigationController

只需进入故事板,选择您启动视图控制器,然后单击编辑器>嵌入并选择导航控制器,然后您的代码将起作用。 这意味着您已经从初始视图控制器为视图控制器设置了导航堆栈。

暂无
暂无

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

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