【问题标题】:Deprecated code in iOS 6 fallback to iOS 5iOS 6 中弃用的代码回退到 iOS 5
【发布时间】:2012-09-22 20:42:05
【问题描述】:

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

- (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 并且没有关闭任何视图,有什么提示吗?

提前谢谢你。

【问题讨论】:

    标签: objective-c xcode ios5 ios6 fallback


    【解决方案1】:

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

    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() 包装器中的位。

    【讨论】:

    • 有时最好测试操作系统的版本并使用它来选择路径。而且无论你如何测试,你都可以设置一个(相对的)全局变量,这样你只测试一次,然后根据(整洁的)全局标志而不是(凌乱的)respondsToSelector 表达式选择哪个路径。
    • 非常感谢蒂姆,解释得很好,它就像一个魅力。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多