dismissModalViewControllerAnimated已弃用 [英] dismissModalViewControllerAnimated deprecated

查看:139
本文介绍了dismissModalViewControllerAnimated已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到XCode 4.5以更新我的iOS应用程序以在iPhone 5的4英寸显示屏上运行,但我收到一个构建错误,说 dismissModalViewControllerAnimated:'已弃用就行:

I've just upgraded to XCode 4.5 to update my iOS app to run on the 4 inch display for the iPhone 5, but I'm getting a build error saying dismissModalViewControllerAnimated:' is deprecated on the line:

[self dismissModalViewControllerAnimated:NO];

我尝试使用完成处理程序(但设置为NULL)更新为推荐的重载:

I've tried updating to the recommended overload with a completion handler (but set to NULL) like this:

[self dismissModalViewControllerAnimated:NO completion:NULL];

但是这一行引发了两个错误:

But then this line throws two errors:

warning: 'TabBarController' may not respond to '-presentModalViewController:animated:completion:'
Instance method '-presentModalViewController:animated:completion:' not found (return type defaults to 'id')

谢谢!

推荐答案

新方法是:

[self dismissViewControllerAnimated:NO completion:nil];

模态一词已被删除;就像呈现API调用一样:

The word modal has been removed; As it has been for the presenting API call:

[self presentViewController:vc animated:NO completion:nil];

原因在 2012 WWDC Session 236 - iOS上视图控制器的演变中讨论过视频。本质上,这个API提供的视图控制器不再是模态的,因为它们正在添加一个完成处理程序,所以现在是重命名它的好时机。

The reasons were discussed in the 2012 WWDC Session 236 - The Evolution of View Controllers on iOS Video. Essentially, view controllers presented by this API are no longer always modal, and since they were adding a completion handler it was a good time to rename it.

回复评论来自Marc:

In response to comment from Marc:


支持所有4.3及以上设备的最佳方法是什么?新的
方法在iOS4中不起作用,但旧方法在iOS6中已弃用。

What's the best way to support all devices 4.3 and above? The new method doesn't work in iOS4, yet the old method is deprecated in iOS6.

我意识到这一点几乎是一个单独的问题,但我认为值得一提,因为不是每个人都有钱每3年升级一次所有设备,所以我们很多人都有一些旧的(5.0之前的)设备。尽管如此,尽管我很难说,但你需要考虑是否值得将目标定在5.0以下。 5.0以下没有许多新的和酷的API。苹果公司不断努力瞄准它们;例如,从Xcode 4.5中删除了armv6支持。

I realize that this is almost a separate question, but I think it's worth a mention since not everyone has the money to upgrade all their devices every 3 years so many of us have some older (pre 5.0) devices. Still, as much as it pains me to say it, you need to consider if it is worth targeting below 5.0. There are many new and cool APIs not available below 5.0. And Apple is continually making it harder to target them; armv6 support is dropped from Xcode 4.5, for example.

要目标低于5.0(只要完成块为零),只需使用方便的 respondsToSelector :method。

To target below 5.0 (as long as the completion block is nil) just use the handy respondsToSelector: method.

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
    [self presentViewController:test animated:YES completion:nil];
} else {
    [self presentModalViewController:test animated:YES];
}

回应Marc的另一条评论:

In response to another comment from Marc:


这可能是我的应用程序中的很多If语句!...我想
考虑创建一个封装了这段代码的类别,
在UIViewControler上创建一个类别让我被拒绝?

That could be quite a lot of If statements in my application!...I was thinking of creating a category that encapsulated this code, would creating a category on UIViewControler get me rejected?

和一个来自Full Decent:

and one from Full Decent:


...有没有办法手动导致它不呈现编译器警告?

...is there a way to manually cause that to not present a compiler warning?

首先,不,在 UIViewController 上创建一个类别本身不会让你的应用被拒绝;除非该类别方法称为私有API或类似的东西。

Firstly, no, creating a category on UIViewController in and of itself will not get your app rejected; unless that category method called private APIs or something similar.

类别方法对于此类代码来说是一个非常好的地方。此外,由于只有一次调用已弃用的API,因此只会有一个编译器警告。

A category method is an exceedingly good place for such code. Also, since there would be only one call to the deprecated API, there would be only one compiler warning.

要解决Full Decent的评论(问题),是的,你可以抑制手动编译警告。 这是指向该主题的答案的链接。类别方法也是抑制编译器警告的好地方,因为您只是在一个地方抑制警告。你肯定不想无缘无故地绕过编译器。

To address Full Decent's comment(question), yes you can suppress compiler warnings manually. Here is a link to an answer on SO on that very subject. A category method is also a great place to suppress a compiler warning, since you're only suppressing the warning in one place. You certainly don't want to go around silencing the compiler willy-nilly.

如果我要为此编写一个简单的类别方法,可能是这样的:

If I was to write a simple category method for this it might be something like this:

@implementation UIViewController (NJ_ModalPresentation)
-(void)nj_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
    NSAssert(completion == nil, @"You called %@ with a non-nil completion. Don't do that!",NSStringFromSelector(_cmd));
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]){
        [self presentViewController:viewControllerToPresent animated:flag completion:completion];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [self presentModalViewController:viewControllerToPresent animated:flag];
#pragma clang diagnostic pop
    }
}
@end

这篇关于dismissModalViewControllerAnimated已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆