Ios 如何覆盖新函数并使其向后兼容?

Ios 如何覆盖新函数并使其向后兼容?,ios,objective-c,cocoa-touch,inheritance,overriding,Ios,Objective C,Cocoa Touch,Inheritance,Overriding,我想覆盖我的UIView子类的setTintColor:函数。在iOS 7中这样做没有问题,因为该功能是从该版本开始引入的。然而,我不知道如何使这个函数向后兼容iOS 6。以下程序将崩溃 - (void)setTintColor:(UIColor *)tintColor { if ([super respondsToSelector:@selector(setTintColor:)]) { // Crashes here with "unrecognized select

我想覆盖我的
UIView
子类的
setTintColor:
函数。在iOS 7中这样做没有问题,因为该功能是从该版本开始引入的。然而,我不知道如何使这个函数向后兼容iOS 6。以下程序将崩溃

- (void)setTintColor:(UIColor *)tintColor
{
    if ([super respondsToSelector:@selector(setTintColor:)]) {
        // Crashes here with "unrecognized selector" in iOS 6, probably because 
        // defining setTintColor: makes respondsToSelector: return YES for all
        // objects in the hiearchy regardless of whether it is defined at its
        // level.
        [super setTintColor:tintColor];
    }
    self.label.textColor = tintColor;
    self.dividerLeft.backgroundColor = tintColor;
    self.dividerRight.backgroundColor = tintColor;
}

你可以把条件改成这个

if ([UIView instancesRespondToSelector:(setTintColor:)])
代码不工作的原因是因为
super
只是
self
的一个特殊版本,并且由于您的子类响应
setTintColor:
它将返回YES