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

How can I modally present a UIViewController on another Storyboard that's embedded in a UINavigationController?

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

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

在主情节提要板上(初始的ViewController 顺便说一句),我有一个 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 Cancel,以关闭模态呈现的视图。

为了获得 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];

但是,这不起作用并抛出 "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller "。

我该如何解决这个问题?

从错误来看,您似乎将 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];