pushViewController 不做任何动作

pushViewController does no action

我有一个表格视图,我想在点击其中一行时转到另一个 vc。我的 didSelectRowAtIndexPath 函数如下所示。打印命令有效并显示右键单击的行。但是当我使用 self.navigationController?.pushViewController 时,它不会使用 playVideo storyBoardId 转到 vc。将其更改为 presentViewController 后,它就可以工作了。我的推送不起作用的代码有什么问题?

谢谢

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

    print("clicked " + String(indexPath.row) )
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("playVideo") as! PlayVideoViewController

    vc.id = self.video[indexPath.row].id

    // Present View as Modal
    //presentViewController(vc as UIViewController, animated: true, completion: nil)
    // Push View
    //self.navigationController?.pushViewController(vc, animated: true)
}

确保您的 ViewController 确实建立在 NavigationController 之上。尝试强制行:

self.navigationController!.pushViewController

如果它崩溃了,你就知道没有设置导航。或者您可以查看故事板,但这是一种快速判断的方法。

我不知道你为什么要这样做。我认为它不必要地复杂。

这应该由您的函数 didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("playVideo", sender: tableView)
}

然后您可以在此函数中指定任意多的转场:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "playVideo") {
        let indexPath:NSIndexPath = tableView.indexPathForSelectedRow!
        let PlayVideoViewController = segue.destinationViewController as! PlayVideoViewController
        //you can pass parameters like project id
        detailVC.projectID = ids[indexPath.row]
    }
}

你的 segue 必须在界面生成器中这样命名:

  • 这是因为您当前所在的视图控制器可能没有导航控制器。
  • 没有 UINavigationController 就无法推送视图控制器。
  • 如果您的要求是推送视图控制器,请执行以下步骤。

    1. 为当前视图控制器嵌入一个导航控制器。(打开故事板 -> 选择你的视图控制器 -> 选择 "Editor" -> "Embed in" -> UINavigationController)
    2. 现在你的代码

    self.navigationController?.pushViewController(vc, animated: true)

会正常工作。