• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

子类化 UI 元素,如 UILabel、UIButton

用户头像
it1352
帮助5

问题说明

我在 CustomLabel 类 中继承了 UILabel.当我尝试使用简单的 UILabel 时遇到问题,我想在将来对其他元素进行子类化.我读过我可以创建 UILabelcategory.这些东西哪个更好?类别还是子类别?

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];

正确答案

#1

这实际上取决于您要实现的目标,尽管类别不能具有属性,但在您的示例中它们不起作用.

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
系列文章
更多 icon
同类精品
更多 icon
继续加载