How to subclass a UILabel?
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 lo开发者_如何学运维ok 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
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.
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.
精彩评论