Subclassing UIRefreshControl but still supporting iOS 5.1?

房东的猫 提交于 2019-12-11 06:46:28

问题


Added a UIRefreshControl to one of my tableviews here, and just used respondsToSelector on the the tableview controller to see if it has the refreshControl property before configuring and adding the UIRefreshControl using NSClassFromString(). Works perfectly and I can continue supporting iOS 5.1 (just without them getting the new control).

However… I want to override the beginRefreshing and endRefreshing methods to dynamically change the tint color of the control. And I figured subclassing UIRefreshControl would be the easiest way of doing this. But how would I do that and still support iOS 5.1?


回答1:


Actually, assuming your base SDK is at least iOS 6.0, you can subclass UIRefreshControl as long as your deployment target is iOS 3.1 or later. That's because in iOS 3.1, support was added for weakly-linked classes.

With weakly-linked classes, if you send a message to a class that is not present in the running OS, it is the same as messaging nil. Thus, instead of using NSClassFromString(), you can just do this:

if ([UIRefreshControl class]) {
    // Use it
}
else {
    // Do something else 
}

This works even when messaging your own subclass of a weakly-linked class. As Apple's "SDK Compatibility Guide" says,

If you subclass a weakly linked class and the superclass is unavailable, then the subclass also appears unavailable.

So you can just do this:

if ([MyRefreshControl class]) {
    MyRefreshControl *control = [[MyRefreshControl alloc] init];
    // Do something with the control
}
else {
    // Do something else 
}

This will work on devices running iOS 5.1 just as well as it works on devices running iOS 6. Your problem is solved.



来源:https://stackoverflow.com/questions/13027250/subclassing-uirefreshcontrol-but-still-supporting-ios-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!