继承 UIRefreshControl 但仍支持 iOS 5.1? [英] Subclassing UIRefreshControl but still supporting iOS 5.1?

查看:25
本文介绍了继承 UIRefreshControl 但仍支持 iOS 5.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处向我的一个 tableview 添加了 UIRefreshControl,并且在使用 NSClassFromString() 配置和添加 UIRefreshControl 之前,只在 tableview 控制器上使用了 RespondsToSelector 以查看它是否具有 refreshControl 属性.完美运行,我可以继续支持 iOS 5.1(只是没有他们获得新的控制).

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).

但是……我想覆盖 beginRefreshing 和 endRefreshing 方法来动态更改控件的色调颜色.我认为子类化 UIRefreshControl 将是最简单的方法.但是我如何做到这一点并且仍然支持 iOS 5.1?

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?

推荐答案

实际上,假设你的基础 SDK 至少是 iOS 6.0,你可以子类化 UIRefreshControl 只要你的部署目标是 iOS 3.1 或之后.这是因为在 iOS 3.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.

对于弱链接类,如果您向正在运行的操作系统中不存在的类发送消息,则与消息 nil 相同.因此,您可以不使用 NSClassFromString(),而是可以这样做:

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 
}

即使在向您自己的弱链接类的子类发送消息时,这也有效.正如 Apple 的 《SDK 兼容性指南》 说,

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.

所以你可以这样做:

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

这将在运行 iOS 5.1 的设备上运行,就像在运行 iOS 6 的设备上运行一样.您的问题已解决.

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

这篇关于继承 UIRefreshControl 但仍支持 iOS 5.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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