-self parentViewController- dismissModalViewController not working on iOS 5

11,910

Solution 1

On iOS 5 you will need to use the presentingViewController selector instead of the parentViewController selector.

Solution 2

-(UIViewController *)getParentViewController{   
    float currentVersion = 5.0;
    float sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (sysVersion >= currentVersion) {
        // iOS 5.0 or later version of iOS specific functionality hanled here 
           return self.presentingViewController;
    }
    else {
        //Previous than iOS 5.0 specific functionality
           return self.parentViewController;

    }
}

Solution 3

The app i have in the store was built using ios SDK 4.3 and uses self.parentViewController dismissModalViewControllerAnimated:YES. It continues to work with IOS 5 devices. I thought it would since it was built on sdk 4.3. Now when i'm updating it with the new xcode and ios 5.0 sdk, it will not work as is and i have to change all the view closing stuff to use the conditional selector workaround mentioned above.(yuck!)

Just thought i'd mention that dismissing from the parent should work on ios 5 (at least in my case with the ios 4.3 sdk). I can't speak for previous sdks or other selectors with parentViewController.

Solution 4

I have built a category that add presentingViewController on iOS 4.

It disables itself on iOS 5.

You can use it seamlessly. Please see backward-modal.

Share:
11,910
Stephen Lynx
Author by

Stephen Lynx

Updated on June 16, 2022

Comments

  • Stephen Lynx
    Stephen Lynx almost 2 years

    So, any clue on this? I had to use [self dismiss modalviewcontroller to dismiss modalviews. Funny fact: when dismissing a tabbarcontroller I could still use the reference to parentviewcontroller, when dismissing a regular viewcontroller, not.

  • Stephen Lynx
    Stephen Lynx over 12 years
    Thanks, mate. I just wish the compiler would give any hint on that, how the hell are people suposed to know it? Read the whole damn documentation?
  • Vinnie
    Vinnie over 12 years
    The problem is that existing 4.x code no longer works in iOS5 devices using parentViewController. Also, if I use presentingViewController, any 4.x code crashes because it doesn't recognize the selector. Please correct me if I'm wrong. I'm starting to get a very bad feeling about iOS5.
  • topace
    topace over 12 years
    Thanks Jason and Vinnie! This iOS 5 is starting to create pain for me :(
  • Joe Regan
    Joe Regan over 12 years
    @Vinnie - It's not the greatest solution but you can always implement a method to conditionally determine what selector should be used to get the presenting/parent VC. -(UIViewController*) getPresentingViewController:(UIImagePickerController*) picker { return ([picker respondsToSelector:@selector(presentingViewController)] ? [picker presentingViewController] : [picker parentViewController]); }
  • Vinnie
    Vinnie over 12 years
    Yes, I am aware of the work-arounds. I ended up using a customParentViewController property that I created myself. My bone to pick is with my previous apps in the app store. They're broken. Some I haven't updated in a long time and didn't really want to dive into the code to fix Apples problems.
  • Ivan Vučica
    Ivan Vučica over 12 years
    Alternatively, [self respondsToSelector:@selector(presentingViewController)];. Also, don't prefix the method with get.
  • Jon Reid
    Jon Reid about 12 years
    +1 for @IvanVučica. Making decisions based on the system version is poor form. Let the object tell you instead.