elevenhuan
elevenhuan

Reputation: 11

How to realize the label text dynamic change?

How to realize the label text dynamic change? For example,

labe.text = @"1"; 

the second is the :

 label.text = @"2";

Upvotes: 1

Views: 208

Answers (1)

Sunil Pandey
Sunil Pandey

Reputation: 7102

You can do this by extending UILabel class.

MyLable.h

#import <UIKit/UIKit.h>
@protocol MyLabelDelegate;
@interface MYLabel : UILabel

@property(nonatomic, unsafe_unretained) id<MyLabelDelegate> delegate;
@end

@protocol MyLabelDelegate <NSObject>

-(void) label:(UILabel *) label didChangeText:(NSString *) string;

@end

My label.m

@implementation MYLabel
@synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setText:(NSString *)text{
    [super setText:text];
    if([self.delegate respondsToSelector:@selector(label:didChangeText:)]){
        [_delegate label:self didChangeText:text];
    }
}
@end

and you can implement MyLabel delegate in your view controller if want to detect change there

Upvotes: 1

Related Questions