开发者

Canceling a modal view & app crashes

I have a confusion on canceling the modal views:

Case 1: I have a navigation view controller and I am presenting a modal开发者_StackOverflow社区 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?


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.


  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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜