UILabel 子类在 Objective-C 中显示为 UILabel

发布于 2025-01-07 20:52:23 字数 1158 浏览 3 评论 0原文

我是一位经验丰富的 C++ 程序员,试图创建我的第一个 UILabel 的 Objective-C 子类,并添加只读属性

// UINumericlabel.h
@interface UINumericLabel : UILabel

// Returns true if the numeric display contains a decimal point
@property (readonly,nonatomic) BOOL hasDecimalPoint;
@end

//  UINumericLabel.m

#import "UINumericLabel.h"

@implementation UINumericLabel
// Returns true if the numeric display contains a decimal point
- (BOOL) hasDecimalPoint;
{
   return [self.text rangeOfString:(@".")].location != NSNotFound;
}
@end

当我尝试引用实例化 UINumericLabel 的 hasDecimalPoint 属性时,我收到错误中止 <代码> 2012-02-20 18:25:56.289 Calculator[10380:207] -[UILabel hasDecimalPoint]: 无法识别 选择器发送到实例 0x684c5c0

在调试器中,它显示我将 UINumericLabel 属性声明为 UILabel * 我是否需要在 UINumericLabel 子类中重写 UILabel 的 (id)init ?我该怎么做?

#import "UINumericLabel.h"

@interface CalculatorViewController : UIViewController <ADBannerViewDelegate>
@property (weak, nonatomic) IBOutlet UINumericLabel *display0;
@end

当我将鼠标悬停在调试器中的 display0P 上时,它说它是 UILabel * 不是 UINumericLabel *

UINumericLabel * display0P = self.display0;

I'm a experienced C++ programmer trying to create my first Objective-C subclass of UILabel with an added read-only property

// UINumericlabel.h
@interface UINumericLabel : UILabel

// Returns true if the numeric display contains a decimal point
@property (readonly,nonatomic) BOOL hasDecimalPoint;
@end

//  UINumericLabel.m

#import "UINumericLabel.h"

@implementation UINumericLabel
// Returns true if the numeric display contains a decimal point
- (BOOL) hasDecimalPoint;
{
   return [self.text rangeOfString:(@".")].location != NSNotFound;
}
@end

When I try to reference the hasDecimalPoint property for an instantiated UINumericLabel I get an abort with error

2012-02-20 18:25:56.289 Calculator[10380:207] -[UILabel hasDecimalPoint]: unrecognized
selector sent to instance 0x684c5c0

In the debugger it shows my declaration of a UINumericLabel property as being a UILabel *
Do I need to override the (id)init for UILabel in my UINumericLabel subclass? How do I do that?

#import "UINumericLabel.h"

@interface CalculatorViewController : UIViewController <ADBannerViewDelegate>
@property (weak, nonatomic) IBOutlet UINumericLabel *display0;
@end

When I hover over display0P in the debugger it says it is a UILabel * not a UINumericLabel *

UINumericLabel * display0P = self.display0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2025-01-14 20:52:23

在 Interface Builder 中选择标签,然后打开 Identity Inspector。在文本字段“Class”中,它可能显示为UILabel。将其更改为新的子类 UINumericLabel

In the Interface Builder select the label and then open the Identity Inspector. In the text field "Class" it probably says UILabel. Change that to be your new subclass, UINumericLabel.

巨坚强 2025-01-14 20:52:23

我同意上面的@Brian,但会添加两件事:(1)不需要声明的属性,除非您计划缓存 BOOL 值。只需在 .h 中使用带有预声明的方法,并且 (2) 在这种情况下不需要子类。更好的方法是扩展,例如: UILabel+JonN.h...

@interface UILabel (JonN)

-(BOOL)hasDecimal;

@end

然后,在 UILabel+JonN.m...

@implementation UILabel (JonN)

// your method as written

@end

我认为这更漂亮,并且解决了您在 IB 中遇到的问题(我认为@布莱恩正确地解决了)。

I agree with @Brian above, but would add two things: (1) no need for a declared property unless you plan to cache the BOOL value. Just use a method with a predeclaration in .h, and (2) no need for a subclass in this case. A better approach would be an extension, like: UILabel+JonN.h...

@interface UILabel (JonN)

-(BOOL)hasDecimal;

@end

Then, in UILabel+JonN.m...

@implementation UILabel (JonN)

// your method as written

@end

This is prettier, I think, and solves the problem you were having with IB (which I think @Brian correctly addressed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文