Tuesday, October 25, 2011

iOS 5 Support for dismissModalViewControllerAnimated

Fixed an odd bug today and submitted v1.3 of Caroline's Cat Game to the store. Turns out that calling dismissModalViewControllerAnimated: for the [self parentViewController] basically stopped doing anything. When Apple makes changes that cause crashes that is bad, but at least we get some kind of alert. This just makes a button stop doing anything. No crash, no error, no nothing.

Anyway, if you are here because your calls to parentViewController have stopped working, here is the code we are now using. It appears that we need to use "presentingViewController" as well as a new call to dismissViewcontrollerAnimated. This all seems to be related to storyboards in iOS 5. Here is a link to the updated UIViewController documentation that talks about parentViewController and presentingViewController and the new dismissViewControllerAnimated. Right here below, is how we have set up our button to respond to being clicked. In the old days (last month) the method just had the   [[self parentViewControllerdismissModalViewControllerAnimated:YES] line but we have to add a little more now.


    if ([[self parentViewController] respondsToSelector:@selector(dismissModalViewControllerAnimated:)]){
        
        [[self parentViewController] dismissModalViewControllerAnimated:YES];
        
    } else {
   
        [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    }


We have wrapped the whole call into an if statement so that we can still use the old way when on an older phone. The respondsToSelector: test is a good way to deal with using old and new code together. Trying to test for a specific version of iOS by name is pretty brittle; always use the respondsTo method instead.

1 comment:

Chip said...

Thanks a bunch for this! I was struggling with the same issue and your fix worked perfectly.

Post a Comment