繁体   English   中英

如何呈现第二个ViewController并解除第一个

[英]How to present a second ViewController & dismiss the first

在父ViewController中使用以下代码,我想在第一个上面显示第二个视图,然后关闭第一个:

// 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”然后显示“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];

尝试

[self dismissViewControllerAnimated:NO completion:nil];

失败了:

[self.navigationController popViewControllerAnimated:YES];

我做了下一步(自我 - 是你的老控制者):

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];
    }];

使用“unwind segue”可能更好:

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。

似乎不能在没有简单显示A的情况下从B到C,这看起来不专业。 但是,您可以将黑色子视图放在A顶部,直到您将其设置为C。

有关代码,请参阅https://stackoverflow.com/a/45579371/218226上的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM