简体   繁体   English

所有呈现的视图控制器都不会被解雇

[英]All the presented view controllers are not dismissed

I have a firstViewController that I display with 我有一个显示的firstViewController

[self.navigationController pushViewController:firstViewController animated:true];

Then when a button in this first VC is clicked, I present a second one: 然后当点击第一个VC中的按钮时,我会出现第二个:

[self presentViewController:secondViewController animated:true completion:nil];

And from this second one I present the third VC as a modal: 从第二个开始,我将第三个VC作为一个模态:

[thirdVC modalPresentationStyle];
[thirdVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];    
[self presentViewController:thirdVC animated:true completion:nil];

The problem is that I want to display the firstVC when the validate button of the last VC is clicked. 问题是我想在单击最后一个VC的验证按钮时显示firstVC。 So I have to dismiss the third and the second one. 所以我不得不解雇第三个和第二个。 I tried this in the validateAction (in the third VC): 我在validateAction尝试了这个(在第三个VC中):

[[self parentViewController] dismissViewControllerAnimated:true completion:nil];
[self dismissViewControllerAnimated:true completion:nil];

But the result is that the thirdVC (the modal one) is dismissed, and from the debugger I can see that the firstVC is covered by the secondVC. 但结果是第三个VC(模态一个)被解除了,从调试器我可以看到第一个VC被第二个VC覆盖。

How can I dismiss the second and the third VC at the same time in order to return to the first one? 如何同时解除第二个和第三个VC才能返回第一个VC? NOTE: I want to push the firstVC after the dismiss of the others because I have to reload the data (in the fristVC viewDidLoad) 注意:我想在解除其他人之后推送第一个VC,因为我必须重新加载数据(在fristVC viewDidLoad中)

From Apple's Documentation : 来自Apple的文档

The presenting view controller is responsible for dismissing the view controller it presented. 呈现视图控制器负责解除它所呈现的视图控制器。 If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal. 如果在呈现的视图控制器本身上调用此方法,UIKit会要求呈现视图控制器处理解雇。

In other words, the third viewController asks the second viewController to dismiss its presented viewController, which is the third one. 换句话说,第三个viewController要求第二个viewController关闭它呈现的viewController,这是第三个。 Then the third viewController tries to dismiss itself, causing UIKit to ask the second viewController to dismiss the third one once more. 然后第三个viewController尝试解散自己,导致UIKit再次请求第二个viewController解除第三个。

You will have to go one layer up and ask the first viewController to dismiss its presented view controller. 你将不得不去一层并要求第一个viewController解雇它呈现的视图控制器。

It is good practice that the third viewController does not know anything about the presentation hierarchy. 优秀的做法是第三个viewController对表示层次结构一无所知。 You can use delegation to tell the first viewController that the task is finished. 您可以使用委托告诉第一个viewController任务已完成。 An exit segue is a very nice alternative if you use storyboards. 如果您使用故事板,退出segue是一个非常好的选择。

You only have to call -dismissViewControllerAnimated:completion: once if you address the first viewController: 你只需要调用-dismissViewControllerAnimated:completion:如果你解决了第一个viewController:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. 如果连续呈现多个视图控制器,从而构建一堆呈现的视图控制器,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈上该子视图上方的所有视图控制器。

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

相关问题 检测何时关闭呈现的视图控制器 - Detect when a presented view controller is dismissed 模态显示/关闭视图控制器时通知? - Notification when view controller is presented modally/dismissed? 有没有办法在 Xcode 11 中找到所有模态呈现的视图控制器? - Is there a way to find all modally presented view controllers in Xcode 11? 视图控制器显示不正确 - View Controllers not being presented correctly Swift:展示了两个视图控制器 - Swift: Two View Controllers presented iOS /自动版式:取消已演示的控制器后,更改为演示视图 - IOS/Autolayout: Change to Presenting View After Presented Controller Dismissed 关闭呈现的视图控制器时如何恢复焦点? - How to restore focus when presented view controller is dismissed? 当通过手势关闭呈现的视图 controller 时如何获得通知? - How to get notified when a presented view controller is dismissed with a gesture? 调用viewWillAppear时显示视图控制器的问题被解除 - Issue with calling viewWillAppear of presenting view controller when presented one is dismissed 呈现MFMessageComposeViewController然后将其关闭后,呈现视图无法正确显示 - Presenting view not appearing properly after MFMessageComposeViewController is presented and then dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM