Subclassing an NSTextField

后端 未结 2 1104
灰色年华
灰色年华 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条回答
  •  闹比i
    闹比i (楼主)
    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:...];
    }
    

提交回复
热议问题