pushViewController不执行任何操作

时间:2016-08-04 19:50:34

标签: ios swift pushviewcontroller

我有一个tableview,我想在其中一行敲击时转到另一个vc。我的didSelectRowAtIndexPath函数如下。 print命令工作并显示右键单击的行。但是当我使用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)
}

3 个答案:

答案 0 :(得分:3)

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

    1. 为当前视图控制器嵌入导航控制器。(打开故事板 - >选择视图控制器 - >选择"编辑器" - >"嵌入" - >的UINavigationController)
    2. 现在你的代码
    3. self.navigationController?.pushViewController(vc,animated:true)

工作正常。

答案 1 :(得分:1)

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

self.navigationController!.pushViewController

如果崩溃,你知道没有设置导航。或者你可以查看你的故事板,但这是一个快速告诉的方​​法。

答案 2 :(得分:-1)

我不知道你为什么这样做。我觉得它不必要地复杂化了。

这应该通过你的函数didSelectRowAtIndexPath:

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

然后你可以在这个函数中指定尽可能多的segue:

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必须在界面构建器中这样命名:

named segue