Objective-c为旧设备添加对新类的支持 [英] Objective-c add support for new classes to old devices

查看:115
本文介绍了Objective-c为旧设备添加对新类的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:每个新iOS都会添加许多新的有用类。例如,UIRefreshControl。我想在iOS5版本中添加对这个类的支持。

The problem: every new iOS adds a lot of new useful classes. For example, UIRefreshControl. I want to add support for this class in iOS5 build.

不酷的解决方案:在所有必须使用UIRefreshControl的类中,我可以检查当前iOS版本并使用内联替换该类,例如:

Not cool solution: In all classes that must use UIRefreshControl, I can check for current iOS version and use inline replacement for that classes, for example:

pseudocode
...

- (void)viewDidLoad
{
    ...

    if([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        self.refreshControl = [[MyCustonRefreshControl_for_iOS5 alloc] init];
    }
    else
    {
        self.refreshControl = [[UIRefreshControl alloc] init];
    }
    ...
}

此解决方案是'很酷,因为我必须在我想要使用最新iOS功能的所有类中添加相同的代码。

This solution is't cool, because I must add same code in all classes where I want to use latest iOS features.

可能很酷的解决方案
1)获取或创建自己的100%兼容类,例如对于UIRefreshControl,您可以使用CKRefreshControl(https://github.com/instructure/CKRefreshControl);
2)使用Objective-C运行时在App启动时将替换类定义为主类。

Possible cool solution: 1) get or create your own 100% compatible class, for example for UIRefreshControl you can use CKRefreshControl (https://github.com/instructure/CKRefreshControl); 2) use Objective-C runtime to define replacement class as main class when App starts.

pseudocode
...

// ios 5 compatibility
#include <objc/runtime.h>
#import "CKRefreshControl.h"
...



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    // pre-ios 6 compatibility
    if([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // register refresh control
        Class clazz = objc_allocateClassPair([CKRefreshControl class], "UIRefreshControl", 0);
        objc_registerClassPair(clazz);
    }

    ...
}

我认为这种方式真的很酷,但这段代码不起作用。

I think that this way is really cool, but this code won't work.

推荐答案

@abuharsky,[CKRefreshControl] https://github.com/instructure/CKRefreshControl 已更新,现在可以满足您的要求。您需要做的就是:

@abuharsky, [CKRefreshControl]https://github.com/instructure/CKRefreshControl has been updated and now does exactly what you are asking for. All you need to do is:

self.refreshControl = [[UIRefreshControl alloc] init];

如果这不适合你,请告诉我们。

Let us know if that doesn't work for you.

这篇关于Objective-c为旧设备添加对新类的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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