Subclassing an NSTextField

后端 未结 2 1103
灰色年华
灰色年华 2021-01-23 08:32

Given all the complex things I seem to cover every day, this appears to be a \"what the heck am I doing wrong that seems to simple?\" scenario!

I would like to subclass

相关标签:
2条回答
  • 2021-01-23 09:14

    Since you can set the background color and text color of an NSTextField in the Attributes inspector of Interface Builder, or you can use -setBackgroundColor: and -setTextColor: programmatically, there should be no need to create a subclass just to change colors. Seems like overkill.

    Edit: If styling a lot of elements is a problem, have you considered using bindings for the colors? Last I checked, NSTextField doesn't have a binding for background color, but since you're using a subclass anyway, you can add a binding for it.

    0 讨论(0)
  • 2021-01-23 09:17

    Preston is correct: to change the colors, you should not subclass, just change textfield properties. (Oh it's NSTextfield, it probably does not have properties then... well, just use the getter and setter methods or configure it correctly in IB.)

    As to

    So why does the color change not occur on the text fields?

    Because you are confusing getters and setters. In your subclass, you have implemented them as getters, where they just return a color. In reality, they should be setter functions (and this is how they are implemented in the guts of NSTextfield): you pass them a color, and they then go and fiddle with whatever internals NSTextfield has, to make the color change happen.

    EDIT: ok, if you are subclassing because you always want to set a specific color, you would do something like

    -(void)viewDidAppear { // or whatever is the Appkit equivalent
        [super setBackgroundColor:...];
    }
    
    0 讨论(0)
提交回复
热议问题