子类化UIRefreshControl但仍支持iOS 5.1?
在此处向我的一个tableview中添加了UIRefreshControl,并在使用NSClassFromString()配置和添加UIRefreshControl之前,使用tableview控制器上的respondsToSelector查看它是否具有refreshControl属性。工作完美,我可以继续支持iOS 5.1(只是他们没有得到新的控制)子类化UIRefreshControl但仍支持iOS 5.1?,ios,subclass,uirefreshcontrol,Ios,Subclass,Uirefreshcontrol,在此处向我的一个tableview中添加了UIRefreshControl,并在使用NSClassFromString()配置和添加UIRefreshControl之前,使用tableview控制器上的respondsToSelector查看它是否具有refreshControl属性。工作完美,我可以继续支持iOS 5.1(只是他们没有得到新的控制) 但是…我想重写beginRefreshing和endRefreshing方法,以动态更改控件的着色颜色。我认为将UIRefreshControl子
但是…我想重写beginRefreshing和endRefreshing方法,以动态更改控件的着色颜色。我认为将UIRefreshControl子类化是最简单的方法。但是,我如何做到这一点并仍然支持iOS 5.1呢?事实上,假设您的基本SDK至少是iOS 6.0,只要您的部署目标是iOS 3.1或更高版本,您就可以将UIRefreshControl子类化。这是因为在iOS3.1中,增加了对弱链接类的支持 对于弱链接类,如果向运行操作系统中不存在的类发送消息,则与消息传递nil相同。因此,您不必使用
NSClassFromString()
,只需执行以下操作:
if ([UIRefreshControl class]) {
// Use it
}
else {
// Do something else
}
if ([MyRefreshControl class]) {
MyRefreshControl *control = [[MyRefreshControl alloc] init];
// Do something with the control
}
else {
// Do something else
}
即使在传递您自己的弱链接类的子类时,这种方法也能起作用。正如苹果公司所说
如果对弱链接类进行子类化,并且父类不可用,则子类也会显示为不可用
所以你可以这样做:
if ([UIRefreshControl class]) {
// Use it
}
else {
// Do something else
}
if ([MyRefreshControl class]) {
MyRefreshControl *control = [[MyRefreshControl alloc] init];
// Do something with the control
}
else {
// Do something else
}
这将适用于运行iOS 5.1的设备,就像它适用于运行iOS 6的设备一样。您的问题已经解决。事实上,假设您的基本SDK至少是iOS 6.0,只要您的部署目标是iOS 3.1或更高版本,您就可以将
UIRefreshControl
子类化。这是因为在iOS3.1中,增加了对弱链接类的支持
对于弱链接类,如果向运行操作系统中不存在的类发送消息,则与消息传递nil相同。因此,您不必使用NSClassFromString()
,只需执行以下操作:
if ([UIRefreshControl class]) {
// Use it
}
else {
// Do something else
}
if ([MyRefreshControl class]) {
MyRefreshControl *control = [[MyRefreshControl alloc] init];
// Do something with the control
}
else {
// Do something else
}
即使在传递您自己的弱链接类的子类时,这种方法也能起作用。正如苹果公司所说
如果对弱链接类进行子类化,并且父类不可用,则子类也会显示为不可用
所以你可以这样做:
if ([UIRefreshControl class]) {
// Use it
}
else {
// Do something else
}
if ([MyRefreshControl class]) {
MyRefreshControl *control = [[MyRefreshControl alloc] init];
// Do something with the control
}
else {
// Do something else
}
这将适用于运行iOS 5.1的设备,就像它适用于运行iOS 6的设备一样。你的问题解决了