如何实现标签文本的动态变化?

时间:2012-12-14 05:33:28

标签: objective-c uilabel

如何实现标签文本的动态变化?例如,

labe.text = @"1"; 

第二个是:

 label.text = @"2";

1 个答案:

答案 0 :(得分:1)

您可以通过扩展UILabel类来完成此任务。

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

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

如果想要检测更改

,您可以在视图控制器中实现MyLabel委托