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 an NSTextField
to change the background color and text color. For simplicity sake (and to help anyone who hasn't ever subclassed anything before), here is the example of my (simplified) subclass MyNSTextFieldSubclass
...
Step 1:
Create the subclass file:
First the header file
@interface MyTextFieldSubclass : NSTextField {
}
@end
And the method file
@implementation MyTextFieldSubclass
-(NSColor *)backgroundColor {
return [NSColor redColor];
}
-(NSColor *)textColor {
return [NSColor yellowColor];
}
@end
Step 2:
Drag an NSTextField
to a window in Interface Builder, select the Identity
tab in the inspector and select the class MyTextFieldSubclass
Step 3:
Save the IB file, build and run the application
Problem
When I run the build, the text field does not reflect the color subclassing. However, I know the subclass is being called because if I add the following method, it gets called on text changes.
-(void)textDidChange:(NSNotification *)notification {
NSLog(@"My text changed");
}
So why does the color change not occur on the text fields?
I know that I can set the color in IB, but for anyone who has dealt with a lot of UI elements that all need the same styling, subclassing makes life way, way easier.
Ironically, I have never had to subclass an NSTextField
before and this one has me stumped.
As usual, any and all help very much appreciated. I'm sure it will turn out to be a "Doh!" moment - just cant see the wood for the trees right now (plus I'm exhausted from watching too much World Cup Football early in the morning which never helps).
=== SOL开发者_开发问答UTION ===
As offered by Jaanus the solution is to put it into the viewWillDraw
method. Thus my (simplified) method would now look like this:
@implementation MyTextFieldSubclass
-(void)viewWillDraw {
[super setBackgroundColor:[NSColor redColor]];
[super setTextColor:[NSColor yellowColor]];
}
@end
Thanks guys for your help.
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:...];
}
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.
精彩评论