【发布时间】:2012-10-26 10:19:26
【问题描述】:
问题:每个新的 iOS 都添加了很多新的有用的类。例如,UIRefreshControl。我想在 iOS5 构建中添加对此类的支持。
不是很酷的解决方案:在所有必须使用 UIRefreshControl 的类中,我可以检查当前的 iOS 版本并为这些类使用内联替换,例如:
pseudocode
...
- (void)viewDidLoad
{
...
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
self.refreshControl = [[MyCustonRefreshControl_for_iOS5 alloc] init];
}
else
{
self.refreshControl = [[UIRefreshControl alloc] init];
}
...
}
这个解决方案并不酷,因为我必须在所有想要使用最新 iOS 功能的类中添加相同的代码。
可能的酷解决方案: 1) 获取或创建自己的 100% 兼容的类,例如对于 UIRefreshControl,您可以使用 CKRefreshControl (https://github.com/instructure/CKRefreshControl); 2)App启动时使用Objective-C运行时定义替换类为主类。
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);
}
...
}
我觉得这种方式很酷,但是这段代码行不通。
【问题讨论】:
标签: objective-c ios5 ios6 runtime