19

I'm working on iOS 6. My application has a standard navigation controller with embedded a CustomViewController. In this controller I create a modal view like this:

-(IBAction)presentModalList:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    StationsListViewController *list = [storyboard instantiateViewControllerWithIdentifier:@"StationsListViewController"];
    [list setStationsData: [self.stationsData allValues]];
    [self presentModalViewController:list animated:YES];
}

The modal controller show perfectly but dismissing generates a warning. The dismiss method in this controller is:

-(IBAction)backToMap
{
    [self dismissModalViewControllerAnimated:YES];
}

The warning generated is Warning:

Attempt to dismiss from view controller < UINavigationController: 0x1ed91620 > while a presentation or dismiss is in progress!

Any clues about that?

Thanks

Sparviero
  • 664
  • 1
  • 7
  • 14
  • Related - http://stackoverflow.com/questions/1412021/iphone-crashing-when-presenting-modal-view-controller – ChrisF Apr 03 '13 at 09:19

5 Answers5

28

I realise this is a late answer but maybe this will help someone else looking for a solution to this, here is what I did:

-(IBAction)backToMap
{
    if (![[self modalViewController] isBeingDismissed])
        [self dismissModalViewControllerAnimated:YES];
}

For me, i found that line of code was being called multiple times, I couldn't find out why so this was the easiest fix.

JDx
  • 2,615
  • 3
  • 22
  • 33
  • 6
    I also ran into this problem. What may be happening is that your `dimissModalViewControllerAnimated:` call may be placed in a callback (IBAction) method that is (directly or indirectly) triggered by tapping the Cancel button on the modal view -- which already handles dismissing the modal view. So this `dismissModalViewControllerAnimated:` may be a duplicate and can be removed. – smileyborg Jan 14 '13 at 18:02
  • 1
    I had a similar problem, but it was caused by a viewController being presented, then almost immediately dismissed (so dismiss was during present-animation.) So I had to check `isBeingPresented`. – Olie Feb 05 '13 at 21:20
  • It's weird. I think @smileyborg is onto something. But I don't see why it's dismissing seemingly automatically? All I did was an unwind action, and I don't need to dismiss it directly? – huggie Mar 04 '13 at 06:48
14

Thanks JDx for getting me on the right track. I adapted it to form this solution, which will remove the warning without using functions that are deprecated in iOS 6:

-(IBAction)backToMap
{
    if (![self.presentedViewController isBeingDismissed]) {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }
}
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
0

I found this approach to be unreliable - say one case in five I'd still see the error.

My solution was to use the completion block to set a flag which controls whether or not it's safe to dismiss - that way you don't need to check whether or not the view is being dismissed.

-(IBAction)presentModalView:(id)sender {
    :
    self.canDismiss = NO;
    [self presentViewController:aVC animated:YES completion:^{ 
      self.canDismiss = YES; 
     }];
    :
}

In the bit of code where the dismiss occurs, just check the flag:

-(void)dismisser {
    :
    if (self.canDismiss) {
      [self dismissViewControllerAnimated:YES completion:nil];
    }
    :
}

Hey presto, no more errors!

JAWZ apps
  • 11
  • 2
0

Targeting iOS6, this is what worked for me:

if (![self.presentedViewController isBeingDismissed]) 
    [self.presentedViewController dismissViewControllerAnimated:YES
                                                     completion:nil];
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
Yoni Hassin
  • 584
  • 1
  • 5
  • 17
0

You can do whatever you want after the completion of the dismiss method as:

-(IBAction)backToMap
{
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 //Do something here
                             }];
}
Robert
  • 5,278
  • 43
  • 65
  • 115
gdm
  • 7,647
  • 3
  • 41
  • 71