对NSTextField进行子类化 [英] Subclassing an NSTextField
问题描述
考虑到我似乎每天都要面对的所有复杂的事情,这似乎是一个我做错了什么,似乎很简单?场景!
我想子类化一个 NSTextField
来改变背景颜色和文本颜色。为了简单起见(为了帮助任何以前没有子类化的人),下面是我的简化子类的例子 MyNSTextFieldSubclass
...
步骤1:
创建子类文件:
首先是头文件
@interface MyTextFieldSubclass:NSTextField {
}
@end
和方法文件
@implementation MyTextFieldSubclass
- (NSColor *)backgroundColor {
return [NSColor redColor];
}
- (NSColor *)textColor {
return [NSColor yellowColor];
}
@end
将 NSTextField
拖到Interface Builder中的窗口,选择身份
标签,并选择类 MyTextFieldSubclass
保存IB文件,构建并运行应用程序
/ strong>
当我运行构建时,文本字段不反映颜色子类化。但是,我知道子类是被调用的,因为如果我添加下面的方法,它被调用文本更改。
- void)textDidChange:(NSNotification *)notification {
NSLog(@My text changed);
}
那么为什么文本字段上不会出现颜色变化?
我知道我可以在IB中设置颜色,但是对于任何处理了很多的UI元素的人来说,它们都需要相同的样式,子类化使得生活方式,方式更容易。
讽刺的是,我从来没有需要继承一个 NSTextField
我很累。
像往常一样,任何和所有的帮助非常感谢。我相信它会变成一个Doh!时间 - 现在只是看不见树木的树木(加上我从看太多的世界杯足球早上从来没有帮助)。
===解决方案===
由Jaanus提供的解决方案是将它放入 viewWillDraw
方法。因此我的(简化)方法现在看起来像这样:
@implementation MyTextFieldSubclass
- )viewWillDraw {
[super setBackgroundColor:[NSColor redColor]];
[super setTextColor:[NSColor yellowColor]];
}
@end
感谢大家的帮助。
Preston是正确的:要改变颜色,你不应该子类,只是更改textfield属性。 (哦,是NSTextfield,它可能没有属性然后...好,只是使用getter和setter方法或在IB中正确配置它。)
对于
那么,为什么文本字段上不会出现颜色变化?
因为你混淆了getter和setters。在你的子类中,你已经实现了它们作为getter,它们只是返回一个颜色。实际上,它们应该是setter函数(这是它们在NSTextfield的内核中实现的方式):你传递一个颜色,然后去处理NSTextfield的内部结构,使颜色发生变化。 p>
编辑:确定,如果你是子类,因为你总是想设置一个特定的颜色,你会做一些像
- (void)viewDidAppear {//或者什么是Appkit的等价物
[super setBackgroundColor:...]
}
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).
=== SOLUTION ===
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:...];
}
这篇关于对NSTextField进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!