如何子类化 UILabel?

发布于 2024-11-17 11:13:09 字数 605 浏览 3 评论 0原文

我创建了一个名为 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 技术交流群。

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

发布评论

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

评论(2

孤独患者 2024-11-24 11:13:09

有什么不完整的?

This, in header:

-(void)text:(NSString *)text;

与 this, in the body: 不匹配,

-(NSString *) text{

    return super.text; 
}

因此,指定为存在于标头中的函数不在正文中。这会生成一个编译器警告,表明实现不完整。

What's incomplete?

This, in the header:

-(void)text:(NSString *)text;

Doesn't match this, in the body:

-(NSString *) text{

    return super.text; 
}

Thus, the function specified as existing in the header is not in the body. That generates a compiler warning that the implementation is incomplete.

烟火散人牵绊 2024-11-24 11:13:09

我认为您最好从一本有关 Objective-C 和 iPhone 编程的书籍或教程开始。无需显式编写这些 setter 和 getter 方法,只需使用 @property@synthesize 即可。但是,为了解决眼前的问题,您的 .h 应为:

-(NSString*)text;
-(void)setText:(NSString*)newText;

并且 .m 应为:

-(NSString*)text{
    return text;
}

-(void)setText:(NSString*)newText {
    text = newText;
}

通常最好将方法从 .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:

-(NSString*)text;
-(void)setText:(NSString*)newText;

and the .m should read:

-(NSString*)text{
    return text;
}

-(void)setText:(NSString*)newText {
    text = newText;
}

Usually it's a good idea to copy and paste the methods from the .h to the .m to ensure that they match exactly.

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