0

I have this custom back button:

- (IBAction)backToMenu:(id)sender {

[self.presentingViewController dismissModalViewControllerAnimated:YES]; 

}

Testing my app in the iOS 6 simulator says dismissModalViewControllerAnimated is deprecated, and I must use dismissViewControllerAnimated instead, so, how I can use the iOS 6 code and fallback to iOS 5

I have tried this:

if([self respondsToSelector:@selector(presentingViewController:animated:completion:)])
    [self.presentingViewController dismissViewControllerAnimated:(YES) completion:nil];
else if([self respondsToSelector:@selector(presentingViewController:animated:)])
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
else
    NSLog(@"Oooops, what system is this ?!!! - should never see this !");

But without results, I'm seeing the NSLog and no view is dismissed, any hints?

Thank you in advance.

roymckrank
  • 689
  • 3
  • 12
  • 27

2 Answers2

6

The selectors you're testing for aren't the same as the selectors you're calling. Try the following:

if([self.presentingViewController respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
    [self.presentingViewController dismissViewControllerAnimated:(YES) completion:nil];
else if([self.presentingViewController respondsToSelector:@selector(dismissModalViewControllerAnimated:)])
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
else
    NSLog(@"Oooops, what system is this ?!!! - should never see this !");

The important difference is that the object you're calling - self.presentingViewController, in this case - is different from the method you're calling on that object. We call the latter a selector, and that's the bit you want to put inside the @selector() wrapper.

Tim
  • 59,527
  • 19
  • 156
  • 165
  • And sometimes it's better to test the version of the OS and use that to select a path. And no matter how you test, you can set a (relatively) global variable so you only test once and then select which path based on the (tidy) global flag rather than the (messy) `respondsToSelector` expression. – Hot Licks Oct 02 '12 at 00:25
  • Thank you very much Tim, very well explained, it worked like a charm. – roymckrank Oct 18 '12 at 23:00
5

Use [self dismissViewControllerAnimated:YES completion:Nil]; for iOS 6

Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27