在presentViewController之后调用pushViewController不起作用

时间:2015-07-23 23:01:56

标签: ios iphone uiviewcontroller uinavigationcontroller presentviewcontroller

我正在呈现我的视图控制器 -

[self.navigationController presentViewController:self.thingContainerViewController animated:YES completion:nil]; //self.navigationController not nil here

这显示了一个UITableView。我想从这里推送导航堆栈上的VC。但是self.navigationController在这一点上是零。知道如何使这项工作吗?

[self.navigationController pushViewController:otherContainer animated:YES]; //self.navigationController is nil at this point

2 个答案:

答案 0 :(得分:4)

您需要在导航控制器中包装您正在呈现的视图控制器,以便能够使用推送和弹出方法。

所以第一步:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.thingContainerViewController];

然后:

[self.navigationController presentViewController:navigationController animated:YES completion:nil];

如果你这样做,你的代码就可以了。

答案 1 :(得分:0)

Swift 3 / Swift 4

首先,您需要设置要显示的导航控制器。之后,在第二个视图控制器上进行导航过程。

  • 类似的示例

       let firstPresentVC = FirstVC(nibName:"FirstVC",bundle:nil)
       let navVC = UINavigationController(rootViewController:firstPresentVC)
       navVC.isNavigationBarHidden = true
       self.present(navVC, animated: true, completion:nil)
    

现在您位于带有导航功能的当前堆栈中

您可以在那之后推动

let secondPushVC = secondPushVC(nibName:"secondPushVC",bundle:nil)
self.navigationController?.pushViewController(secondPushVC, animated: true)