ios Swift 中的 Objective C Setter 覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25005259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:35:31  来源:igfitidea点击:

Objective C Setter overriding in Swift

iosuibuttonswift

提问by Ezimet

I need to override the setter of UIViews highlighted property in my custom UIButton subclass ;

我需要在我的自定义 UIButton 子类中覆盖 UIViews 突出显示属性的设置器;

Objective C

目标 C

@property(nonatomic,getter=isHighlighted) BOOL highlighted; 

overridden like this

像这样覆盖

- (void) setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = UIColorFromRGB(0x387038);
    }
    else {
        self.backgroundColor = UIColorFromRGB(0x5bb75b);
    }

   [super setHighlighted:highlighted];

}

Swift

迅速

var highlighted: Bool 

I tried:

我试过:

var highlighted: Bool {

   get{ return false }

   set {

        if highlighted {

       self.backgroundColor = UIColor.whiteColor() 
       //Error "Use unresolved identifier     'self'"

         I can't set the background color from value type in here 
        , can't call self.backgroundColor in this value type , 
         can't call super too because this is a value type , doesn't work 
        }

        }

   }

How and where should implement this method in Swift to get the same result . any idea?

在 Swift 中应该如何以及在何处实现此方法以获得相同的结果。任何的想法?

回答by King-Wizard

In Swiftthe solution above worked for me, but I had to omitthe Bool = true:

斯威夫特上述解决方案为我工作,但我不得不省略布尔=真

import UIKit

class CustomUIButtonForUIToolbar: UIButton {

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        super.drawRect(rect)

        self.layer.borderColor = UIColor.blueColor().CGColor
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 5.0
        self.clipsToBounds = true
        self.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)

        self.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
    }

    override var highlighted: Bool {
        didSet {

            if (highlighted) {
                self.backgroundColor = UIColor.blueColor()
            }
            else {
                self.backgroundColor = UIColor.clearColor()
            }

        }
    }

}

回答by Ethan

There are a couple things wrong here, but this solution should help you...

这里有一些问题,但是这个解决方案应该可以帮助你......

In your case, since you do not really want computed values for the variable highlighted, but rather all you want is to know when highlighted changed, you should use willSetor didSet

在您的情况下,由于您并不真正想要变量highlight 的计算值,而您只想知道突出显示何时发生变化,您应该使用willSetdidSet

for your case, didSet.

对于您的情况,didSet

it looks like this

它看起来像这样

var highlighted:Bool = false {
    didSet {
        // You can use 'oldValue' to see what it used to be,
        // and 'highlighted' will be what it was set to.
        if highlighted
        {
            self.backgroundColor = UIColor.whiteColor()
        } else
        {
            self.backgroundColor = UIColor.blackColor()
        }
}
}

Keep in mind that the initial value is set to false BUT initializing the variable does not call the didSet block (i don't think), so default initialize this view with backgroundColor black... or whatever.

请记住,初始值设置为 false 但初始化变量不会调用 didSet 块(我不认为),因此默认使用 backgroundColor black ... 或其他方式初始化此视图。

the Swift ibook has some good tips about set, get, didSet and willSet, around page 250 ish.

Swift ibook 有一些关于 set、get、didSet 和 willSet 的好技巧,大约在第 250 页左右。

Let me know if your error remains, (and if so you should maybe post when you set this variable & the class headers and stuff, may not be enough information. Also, are you using xcode6-beta4?)

如果您的错误仍然存​​在,请告诉我,(如果是这样,您应该在设置此变量和类标题和其他内容时发布信息,可能没有足够的信息。另外,您是否使用 xcode6-beta4?)

回答by codester

You are using computed propertiesinstead use stored property.As there is no UIColorFromRGBprovided in swift so i have written mine

您正在使用,computed properties而不是使用stored property。因为UIColorFromRGBswift 中没有提供所以我写了我的

class ViewSubClass:UIView{

var highlighted: Bool = true {

   didSet {

      if (highlighted) {
    self.backgroundColor = UIColorFromRGB(0x387038);
      }
      else {
         self.backgroundColor = UIColorFromRGB(0x5bb75b);
      }
   }


}


init(frame: CGRect) {

    super.init(frame: frame)
}

func UIColorFromRGB(rgbValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
 }
}