如何实现标签文字动态变化?例如,
labe.text = @"1";
第二个是:
label.text = @"2";
您可以通过扩展 UILabel 类来做到这一点。
MyLabel.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
我的标签.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 委托