【问题标题】:How do you override new function and make it backwards compatible?您如何覆盖新功能并使其向后兼容?
【发布时间】:2014-03-27 22:07:10
【问题描述】:

我想为我的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;
}

【问题讨论】:

    标签: ios objective-c cocoa-touch inheritance overriding


    【解决方案1】:

    你可以把条件改成这个

    if ([UIView instancesRespondToSelector:(setTintColor:)])
    

    您的代码不起作用的原因是因为super 只是self 的一个特殊版本,并且由于您的子类响应setTintColor: 它会返回YES。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-22
      • 2016-09-29
      • 1970-01-01
      • 2022-06-11
      • 2017-10-13
      • 2021-10-09
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多