子类化 UI 元素,如 UILabel、UIButton
问题说明
我在 CustomLabel 类
中继承了 UILabel
.当我尝试使用简单的 UILabel
时遇到问题,我想在将来对其他元素进行子类化.我读过我可以创建 UILabel
的 category
.这些东西哪个更好?类别还是子类别?
I am subclassing an UILabel
in a CustomLabel class
. I have problems when I try to use a simple UILabel
and I will like to subclass other elements in a future. I have read that I can create a category
of UILabel
. Which of this things is better? category or subclass?
这是我尝试子类化的代码.它在 setFont
方法中失败.
Here is my code trying to subclass. It fails in setFont
method.
@interface WPCustomLabel : UILabel
@property (strong, nonatomic) UIColor *color;
@property (strong, nonatomic) UIFont *font;
@end
#import "WPCustomLabel.h"
@implementation WPCustomLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
-(void)setColor:(UIColor *)color
{
self.color = color;
}
-(void)setFont:(UIFont *)font
{
self.font = font;
}
@end
我在我的 ViewController 中调用这个 CustomLabel.
and I call this CustomLabel in my ViewController.
@property (strong, nonatomic) WPCustomLabel *titleLbl;
titleLbl = [[WPCustomLabel alloc] initWithFrame:CGRectMake(75, 25, 200, 14)];
[titleLbl setTextColor:[UIColor blackColor]];
[titleLbl setFont:[UIFont systemFontOfSize:14]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setText:@"Here I AM"];
[self.view addSubview:titleLbl];
正确答案
这实际上取决于您要实现的目标,尽管类别不能具有属性,但在您的示例中它们不起作用.
It really depends what you are trying to achieve, categories cannot have properties though so in your example they would not work.
您的问题是在 setter 中您正在调用 setter:
Your issue here is that in the setters you are recalling the setters:
-(void)setFont:(UIFont *)font
{
self.font = font;
}
编译为(等同于):
-(void)setFont:(UIFont *)font
{
[self setFont:font];
}
您应该能够看到这个问题.一旦你调用它,就无法摆脱这个方法.你在这里混淆了属性和实例变量.覆盖 setter 不应通过属性设置,而应直接设置为实例变量.所以:
You should be able to see this problem. There is no getting out of this method once you call it. You are confusing properties and instance variables here. Overriding setters should not set via the property but rather directly to the instance variable. So:
// LOOK AT EDIT -- Do not do this for 'font'
-(void)setFont:(UIFont *)font
{
_font = font;
}
我没有直接思考.由于您是 UILabel
的子类,因此您已经拥有 font
属性.你不应该直接指定它,因为你的超类 (UILabel
) 已经有了它.所以摆脱那些属性声明.话虽如此,如果不需要 color
,类别可能是您更好的解决方案.无论如何,您可以像这样覆盖 setFont:
方法:
I was not thinking straight. Since you are subclassing UILabel
you already have a font
property. You should not specify this directly since your super class (UILabel
) already has it. So get rid of that property declarations. That being said, if color
is not needed, a category will probably be a better solution for you. Regardless, you can override your setFont:
method like this:
-(void)setFont:(UIFont *)font
{
[super setFont:font];
// do custom stuff
}
并且由于 color
不是 UILabel
属性,您应该通过实例变量设置它(如上:_color = color
),请执行不要在这个 setter 上调用 super,因为 UILabel
不响应它.
And since color
is not a UILabel
property you should set it via the instance variable (like above: _color = color
), do not call super on this setter since UILabel
does not respond to it.
super
调用正在调用 UILabel
的实现,因为您是它的子类.
The super
call is calling UILabel
's implementation since you are a subclass of that.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcbfige
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20