UILabel 子类在 Objective-C 中显示为 UILabel
我是一位经验丰富的 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
selector sent to instance 0x684c5c0
2012-02-20 18:25:56.289 Calculator[10380:207] -[UILabel hasDecimalPoint]: unrecognized
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 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
.我同意上面的@Brian,但会添加两件事:(1)不需要声明的属性,除非您计划缓存 BOOL 值。只需在 .h 中使用带有预声明的方法,并且 (2) 在这种情况下不需要子类。更好的方法是扩展,例如: UILabel+JonN.h...
然后,在 UILabel+JonN.m...
我认为这更漂亮,并且解决了您在 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...
Then, in UILabel+JonN.m...
This is prettier, I think, and solves the problem you were having with IB (which I think @Brian correctly addressed).