简体   繁体   English

在没有导航控制器的情况下关闭多个模态视图

[英]Dismiss multiple modal views without a navigation controller

I didn't plan ahead properly for view navigation in my app, so it's possible for the user to go through a loop that just stacks modal views on top of each other, and from there the root view controller is only accessible through the time-consuming process of manually dismissing each and every repeated view. 我没有正确地计划在我的应用程序中进行视图导航,所以用户可以通过一个循环,只是将模态视图叠加在一起,从那里根视图控制器只能通过时间访问 - 消耗手动解除每个重复视图的过程。

I can't just go ahead and use popToRootViewControllerAnimated: without a navigation controller on top of it all (calling presetModalViewController:animated: repeatedly seemed like a good idea at the time), so unless there's something Google is hiding from me, I'm completely lost. 我不能继续使用popToRootViewControllerAnimated:在它之上没有导航控制器(调用presetModalViewController:animated:当时反复似乎是一个好主意),所以除非谷歌躲避我,否则我是完全迷失了。

Rewriting half the navigation code is not ideal, but if that really is the only option, I'll give it a shot. 重写导航代码的一半并不理想,但如果真的是唯一的选择,我会试一试。
Avoiding that would certainly be preferable. 避免这种情况肯定会更可取。

No idea what code would be helpful, if any. 不知道哪些代码会有用,如果有的话。 It's just presenting/dismissing modal view controllers with a few subclasses of UIViewController 它只是使用UIViewController的几个子类来呈现/解除模态视图控制器

answer A: easy answer; 回答 - 答:简单的答案;

just call dismissModalViewController:animated: on the view that you want to see. 只需在您要查看的视图上调用dismissModalViewController:animated:

answer B: real answer; 答案B:真正的答案;

It will not be hard for you to re-factor as a Navigation Controller app, I would start a new project that is a Navigation app, and look at the methods in the application delegate, and emulate that behavior. 您将重新考虑作为导航控制器应用程序并不难,我将启动一个新项目,即导航应用程序,并查看应用程序委托中的方法,并模拟该行为。

then when you would normally present, just push ( [self.navigationController pushViewController: controller animated: YES] ) and your dismiss will become a pop ( [self.navigationController popViewControllerAnimated: YES] ) 然后当你正常出现时,只需按下( [self.navigationController pushViewController: controller animated: YES] )你的解雇将成为一个pop( [self.navigationController popViewControllerAnimated: YES]

Hope that helps 希望有所帮助

Both @Grady's answers are the right ones. @ Grady的答案都是正确的。 A look at the documentation for -dismissModalViewController:animated: tells you: 看一下-dismissModalViewController的文档:animated:告诉你:

If you present several modal view controllers in succession, and thus build a stack of modal 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. 如果连续呈现多个模态视图控制器,从而构建一组模态视图控制器,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈上该子视图上方的所有视图控制器。 When this happens, only the top-most view is dismissed in an animated fashion; 发生这种情况时,只有最顶层的视图以动画方式被删除; any intermediate view controllers are simply removed from the stack. 任何中间视图控制器都可以从堆栈中删除。

You should use -dismiss... rather than popping the controller from the nav stack, since modal controllers may not even be part of the navigation stack. 您应该使用-dismiss ...而不是从导航堆栈中弹出控制器,因为模态控制器甚至可能不是导航堆栈的一部分。 Nevertheless, if you find that your app should have been a navigation-based app, then just take the time to make it so. 然而,如果您发现您的应用应该是基于导航的应用,那么只需花时间来实现它。 If that's a lot of work, it's probably work that needs to be done anyway. 如果这是很多工作,那么无论如何都可能需要完成工作。

更改UIViewController中的视图属性不起作用?

Okay, now I got it. 好的,现在我明白了。 Below the line is what I should never have done. 线下是我不应该做的。 Rather than trying to contain a loop-grown stack, I should have just blocked it to begin with. 而不是试图包含一个循环增长的堆栈,我应该刚刚开始阻止它。

That is, the "Load" button no longer spawns another view. 也就是说,“加载”按钮不再产生另一个视图。 Instead, it dismisses the current view, which can only ever be a child of the Load view, giving the illusion of a new view and completely removing the problem of an uncontrollably growing stack. 相反,它取消了当前视图,它只能是Load视图的子视图,给出了新视图的错觉并完全消除了不受控制地增长的堆栈的问题。


Again, this is the wrong option: 同样,这是错误的选择:

A global, a few #define 'd strings, and some if/else stacks, and the lack of a navigation controller is not a problem. 全局,一些#define '字符串,以及一些if / else堆栈,以及缺少导航控制器不是问题。

If anyone is interested in what I've done to achieve this (and/or how much fun it'll be to modify later), I'll drop some of the it into this answer. 如果有人对我为实现这一目标所做的事情感兴趣(和/或稍后修改会有多大的乐趣),我会将其中的一部分放入此答案中。 It's not pretty and it was a pain to write (mostly because the new code spans four files and breaks quietly), but it does exactly what I want. 它并不漂亮,写起来很痛苦(主要是因为新代码跨越四个文件并且静静地断开),但它确实完全符合我的要求。

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

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