如何子类化 UILabel?
我创建了一个名为 CommaLabel 的 UILABEL 简单子类,它最终会将逗号插入到数字字符串中,就像苹果计算器那样。编译器说我的实现不完整。愚蠢的问题:缺少什么? (我也不明白我在这里必须对内存管理做什么:-/)(我可能最终只是在视图控制器中实现处理代码,但我只是想看看它看起来如何在此刻...)
#import <Foundation/Foundation.h>
@interface CommaLabel : UILabel
-(void)text:(NSString *)text;
-(void)setText:(NSString*)text;
@end
#import "CommaLabel.h"
@implementation CommaLabel
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(NSString *) text{
return super.text;
}
-(void)setText:text
{
super.text=text;
}
@end
I made a simple subclass of UILABEL called CommaLabel that will eventually insert commas into a numeric string, like apple's calculator does. The compiler says my implementation is incomplete. Stupid question: what's missing? (I also don't understand what I have to do regarding memory management in here :-/) (i'm probably going to end up just implementing the processing code in the view controller but i just want to see how this would look anyway at this point...)
#import <Foundation/Foundation.h>
@interface CommaLabel : UILabel
-(void)text:(NSString *)text;
-(void)setText:(NSString*)text;
@end
#import "CommaLabel.h"
@implementation CommaLabel
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(NSString *) text{
return super.text;
}
-(void)setText:text
{
super.text=text;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么不完整的?
This, in header:
与 this, in the body: 不匹配,
因此,指定为存在于标头中的函数不在正文中。这会生成一个编译器警告,表明实现不完整。
What's incomplete?
This, in the header:
Doesn't match this, in the body:
Thus, the function specified as existing in the header is not in the body. That generates a compiler warning that the implementation is incomplete.
我认为您最好从一本有关 Objective-C 和 iPhone 编程的书籍或教程开始。无需显式编写这些 setter 和 getter 方法,只需使用
@property
和@synthesize
即可。但是,为了解决眼前的问题,您的 .h 应为:并且 .m 应为:
通常最好将方法从 .h 复制并粘贴到 .m 以确保它们完全匹配。
I think you would do well to start with a book or tutorial about Objective-C and iPhone programming. There is no need to write these setter and getter methods out explicitly instead of using
@property
and@synthesize
. However, to address the immediate problems, your .h should read:and the .m should read:
Usually it's a good idea to copy and paste the methods from the .h to the .m to ensure that they match exactly.