Deprecated code in iOS 6 fallback to iOS 5

橙三吉。 提交于 2019-12-09 01:57:48

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12682768/deprecated-code-in-ios-6-fallback-to-ios-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!