Dave Kliman
Dave Kliman

Reputation: 471

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 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

Upvotes: 2

Views: 6093

Answers (2)

PengOne
PengOne

Reputation: 48398

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.

Upvotes: 1

Steven Fisher
Steven Fisher

Reputation: 44856

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.

Upvotes: 1

Related Questions