pushViewController不执行任何操作

Hos*_* Ap 1 pushviewcontroller ios swift

我有一个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)
}
Run Code Online (Sandbox Code Playgroud)

Bha*_*ath 5

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

    1. 为当前视图控制器嵌入一个导航控制器。(打开情节提要->选择视图控制器->选择“编辑器”->“嵌入”-> UINavigationController)
    2. 现在您的代码

    self.navigationController?.pushViewController(vc,动画:true)

会很好的。