如何在UINavigationController中嵌入的另一个Storyboard上以模态方式呈现UIViewController?

时间:2015-06-07 21:43:55

标签: ios objective-c cocoa-touch uiviewcontroller

...并将其嵌入导航控制器中。

我有一个故事板,我们称之为MainStoryboard。

在主故事板上(也不是initialViewController BTW),我有一个ViewController,我们称之为ViewControllerZ,嵌入在NavigationController中,让我们称之为NavigationControllerZ。

用户点击按钮后......我有......

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewControllerZ *vcZ = (ViewControllerZ *)[storyboard instantiateViewControllerWithIdentifier:@"ViewControllerZ"];
vcZ.delegate = self;
vcZ.blah = blah;
[self.navigationController presentViewController:vcZ animated:YES completion:nil];

然而,这并没有为我提供所需的NavigationController。我需要来自MainStorybard的NavigationController,因为ViewControllerZ上的NavBar有一个UIBarButtonItem取消,以解除模态显示的视图。

所以为了获得我试过的NavigationController ......

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NavigationViewControllerA *navVcZ = (NavigationViewControllerZ *)[storyboard instantiateViewControllerWithIdentifier:@"NavigationViewControllerZ"];
ViewControllerZ *vcZ  = (ViewControllerZ *)navVcZ.topViewController;
vcZ.delegate = self;
vcZ.blah = blah;
[self.navigationController presentViewController:vcZ animated:YES completion:nil];

然而,由于未捕获的异常" NSInvalidArgumentException'原因:'应用程序尝试以模态方式呈现活动控制器",但这不起作用并引发终止应用程序。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

从错误中可以看出,您将navVcZ添加为子视图控制器,如下所示:

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

您可以直接展示navVcZ

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NavigationViewControllerA *navVcZ = (NavigationViewControllerZ *)[storyboard instantiateViewControllerWithIdentifier:@"NavigationViewControllerZ"];
ViewControllerZ *vcZ  = (ViewControllerZ *)navVcZ.topViewController;
vcZ.delegate = self;
vcZ.blah = blah;
[self.navigationController presentViewController:navVcZ animated:YES completion:nil];