0

我有这个自定义后退按钮:

- (IBAction)backToMenu:(id)sender {

[self.presentingViewController dismissModalViewControllerAnimated:YES]; 

}

在 iOS 6 模拟器中测试我的应用程序说dismissModalViewControllerAnimated 已被弃用,我必须改用dismissViewControllerAnimated,所以,我如何使用iOS 6 代码并回退到iOS 5

我试过这个:

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 !");

但是没有结果,我看到 NSLog 并且没有视图被驳回,有什么提示吗?

先感谢您。

4

2 回答 2

6

您正在测试的选择器与您正在调用的选择器不同。尝试以下操作:

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 !");

重要的区别在于您正在调用的对象-self.presentingViewController在这种情况下 - 与您在该对象上调用的方法不同。我们称后者为选择器,这就是您要放入@selector()包装器中的位。

于 2012-10-02T00:03:51.893 回答
5

使用 [self dismissViewControllerAnimated:YES 完成:Nil]; 适用于 iOS 6

于 2012-10-12T06:46:33.557 回答