如何呈现第二个视图控制器并关闭第一个视图控制器

9

在父视图控制器中使用以下代码,我想在第一个视图的顶部呈现第二个视图,然后关闭第一个视图:

// Animates the next questionViewController using the first questionViewController
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil];

// Dismiss the first questionViewController
[previousView dismissViewControllerAnimated:NO completion:nil];

运行时,第二个视图呈现出来,但第一个视图不会消失。


previousView是导航控制器堆栈上的一个视图控制器,还是以模态方式显示的? - Raphael Ayres
是的,它以模态方式显示。我无法弄清楚为什么它无法解除。 - Tim Windsor Brown
从您的代码来看,它只使用了presentViewController:animated,而没有使用presentModalViewController:animated。 - Raphael Ayres
从你的代码来看,它只使用了presentViewController:animated:completion:,而没有使用presentModalViewController:animated:。请注意消息中间的Modal单词。除非presentViewController是一个自定义方法,在这种情况下,我们需要更多的代码。 - Raphael Ayres
根据您的代码,它只使用了presentViewController:animated:completion:,而没有用到presentModalViewController:animated:。请注意消息中间的Modal单词。除非presentViewController是一个自定义方法,在这种情况下,我们还需要更多的代码。 - Raphael Ayres
5个回答

12
你需要先关闭"previousView",然后再展示"nextQuestionViewController":
// Dismiss the first questionViewController
[previousView dismissViewControllerAnimated:NO completion:nil];

// Animates the next questionViewController using the first questionViewController
[previousView presentViewController:nextQuestionViewController animated:YES completion:nil];

2
按照此答案进行操作会导致以下异常:Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'Application tried to present modally an active controller <FirstViewController: 0x71cf520>.' - Jade
你不觉得使用一个视图控制器来呈现另一个视图控制器会很容易出错吗?因为你知道它本身在某个时刻会被释放。我宁愿使用[[UIApplication sharedApplication].keyWindow.rootViewController或类似的方法来呈现nextQuestionViewController,正如@Mozilla所建议的那样。 - atulkhatri

5
尝试
[self dismissViewControllerAnimated:NO completion:nil];

如果无法实现上述目标:
[self.navigationController popViewControllerAnimated:YES];

我正在尝试再次解决这个问题 - 使用这两种方法都无法在呈现第一个视图控制器后关闭第二个视图控制器 - 我是否还遗漏了其他东西? - Tim Windsor Brown
正如Raphael所说,您应该按照以下方式呈现第二个视图控制器:[previousView presentModalViewController: nextQuestionViewController animated:YES]; - ader

2
我接下来做了以下事情(self - 是您的旧控制器):
UIStoryboard *storyboard = self.storyboard;

[self dismissViewControllerAnimated:YES
                         completion:^{
        UIViewController *newController = [storyboard instantiateViewControllerWithIdentifier:@"newControllerStoryboardId"];
        newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:newController animated:YES completion:nil];
    }];

0

最好使用“取消返回连线”:

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8

https://developer.apple.com/library/archive/technotes/tn2298/_index.html

它允许您同时解除多个视图控制器,而无需知道堆栈中有多少个视图控制器。并且在没有呈现的视图控制器具有特殊知识的情况下,它不需要知道需要导航回哪里(即您不必直接到窗口的根视图控制器)。

假设您有一个名为VC1的根视图控制器,第一个模态是VC2,第二个模态是VC3。

在VC1中,您将实现一个名为(例如)unwindToRootIBAction。然后,在VC3的故事板中,您将您的完成按钮连接到Exit对象,并选择unwindToRoot操作。

当按下该按钮时,系统将解除所有必须带您返回VC1的视图控制器。

因此,基本上您只是让所有这些VC堆叠起来,当您完成所有操作后,您就可以解除所有内容以返回到根VC。


-2

看起来似乎不可能在不短暂地显示 A 的情况下从 B 前往 C,这看起来不太专业。然而,您可以将黑色子视图放置在 A 的顶部,直到您动画到 C。

有关代码,请参见我的答案 https://dev59.com/KIDba4cB1Zd3GeqPASR5#45579371


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接