取消模态视图&应用程序崩溃

发布于 2024-10-15 16:03:35 字数 292 浏览 7 评论 0原文

我对取消模态视图感到困惑:

情况 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-10-22 16:03:35

处理模态视图控制器的一个明智的方法是向我们发出通知,通知呈现它的类释放它。通常,您使用与此类似的代码来显示模式视图。

SomeClass *yourViewController = [[SomeClass alloc] initWithNibName:@"SomeClass" bundle:nil];
[self presentModalViewController: yourViewController animated: YES];
[yourViewController release];

使用上面的代码,您的模态视图最终的保留计数应该为 1。当您关闭它时,父视图将释放它,并将其从内存中清除。模态视图中的“关闭”按钮应执行如下所示的代码:

- (void)dismissSelf{
  [[NSNotificationCenter defaultCenter] postNotifivationName:@"I'm done" object:self];
}

返回父视图控制器,您应该侦听此通知,然后在发布通知时关闭模态视图。

也就是说,回答你的问题:

  1. 模态视图控制器永远不会自行消失。发布通知,然后让导航控制器处理它。

  2. 在关闭第二个模态视图之前,您无法关闭第一个模态视图。如果这样做,您将收到 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.

SomeClass *yourViewController = [[SomeClass alloc] initWithNibName:@"SomeClass" bundle:nil];
[self presentModalViewController: yourViewController animated: YES];
[yourViewController release];

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:

- (void)dismissSelf{
  [[NSNotificationCenter defaultCenter] postNotifivationName:@"I'm done" object:self];
}

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:

  1. A modal view controller never dismisses itself. Post a notification and then let the navigation controller handle it.

  2. 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.

宫墨修音 2024-10-22 16:03:35
  1. 你应该关闭模态视图
    控制器。
  2. 你应该将其从
    第二个模态视图控制器。

应用程序崩溃是因为当您尝试关闭模式视图控制器时,相应视图控制器的范围丢失,可能是您在关闭之前释放了视图控制器

  1. you should dismiss the modal view
    controller.
  2. you should dismiss it from the
    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

撩起发的微风 2024-10-22 16:03:35

您始终从呈现它的控制器中关闭模态视图(使用dismissModalViewControllerAnimated)。所以:

  1. 导航控制器中
  2. 在第一个模态视图控制器的

You always dismiss the modal view from the controller, where you presented it (with dismissModalViewControllerAnimated). So:

  1. in the navigation controller
  2. in the first modal view controller
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文