取消模态视图&应用程序崩溃
我对取消模态视图感到困惑:
情况 1:我有一个导航视图控制器,并且我正在从该导航视图控制器呈现一个模态视图控制器。现在,当我要从哪里取消这个模态视图时,我应该从哪里调用dismissModalView方法——导航视图控制器还是模态视图控制器?
情况 2:我有一个模态视图控制器,并且我正在从第一个模态视图控制器呈现另一个模态视图控制器。现在,当我要从哪里取消第二个模态视图时,我应该从哪里调用dismissModalView方法——第一个模态视图控制器还是第二个模态视图控制器?
从错误的地方取消它也会导致应用程序崩溃吗?
I have a confusion on canceling the modal views:
Case 1: I have a navigation view controller and I am presenting a modal view controller from this navigation view controller. Now, when I am to cancel this modal view from where should I call the dismissModalView method -- navigation view controller or the modal view controller?
Case 2: I have a modal view controller and I am presenting another modal view controller from first modal view controller. Now, when I am to cancel second modal view from where should I call the dismissModalView method -- frist modal view controller or the second modal view controller?
Will canceling it from a wrong place cause a app crash also?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理模态视图控制器的一个明智的方法是向我们发出通知,通知呈现它的类释放它。通常,您使用与此类似的代码来显示模式视图。
使用上面的代码,您的模态视图最终的保留计数应该为 1。当您关闭它时,父视图将释放它,并将其从内存中清除。模态视图中的“关闭”按钮应执行如下所示的代码:
返回父视图控制器,您应该侦听此通知,然后在发布通知时关闭模态视图。
也就是说,回答你的问题:
模态视图控制器永远不会自行消失。发布通知,然后让导航控制器处理它。
在关闭第二个模态视图之前,您无法关闭第一个模态视图。如果这样做,您将收到 EXC_BAD_ACCESS 错误。将第二个模态视图视为第一个模态视图的“内部”。如果第一个被解散,第二个也会被拖走,但它并没有被解散。
An advisable way to handle modal view controllers is to us notifications to inform the class that presented it to release it. Generally, you use code similar to this to show a modal view.
With the above code, your modal view should end up with a retain count of 1. When you dismiss it, the parent view will release it and it will be purged from memory. Your "close" button in your modal view should execute code that looks like this:
Back in your parent viewcontroller, ou should listen for this notification and then dismiss the modal view when the notification is posted.
That said, to answer your questions:
A modal view controller never dismisses itself. Post a notification and then let the navigation controller handle it.
You can't dismiss the first modal view until the second one has been dismissed. If you do, you will get a EXC_BAD_ACCESS error. Think of the second modal view as "inside" the first one. If the first is dismissed, the second one will be dragged away with it, but it hasn't been dismissed.
控制器。
第二个模态视图控制器。
应用程序崩溃是因为当您尝试关闭模式视图控制器时,相应视图控制器的范围丢失,可能是您在关闭之前释放了视图控制器
controller.
second modal view controller.
the app crashes because when you trying to dismiss the modal view controller , the scope of corresponding view controller is lost, may be u released the view controller before dismissing
您始终从呈现它的控制器中关闭模态视图(使用dismissModalViewControllerAnimated)。所以:
You always dismiss the modal view from the controller, where you presented it (with dismissModalViewControllerAnimated). So: