Ios 如何动态更改UILabel文本?
我想更改UILabel的文本。 但是3个标签的文本是相同的,所以我想知道我是否可以做一些事情,比如存储相关UILabel的实例并编写通用字符串格式来更改文本 这就是我的IBOutlet:Ios 如何动态更改UILabel文本?,ios,objective-c,Ios,Objective C,我想更改UILabel的文本。 但是3个标签的文本是相同的,所以我想知道我是否可以做一些事情,比如存储相关UILabel的实例并编写通用字符串格式来更改文本 这就是我的IBOutlet: @property (strong, nonatomic) IBOutlet UILabel *pushupDetails; @property (strong, nonatomic) IBOutlet UILabel *situpDetails; @property (strong, nonatomic) I
@property (strong, nonatomic) IBOutlet UILabel *pushupDetails;
@property (strong, nonatomic) IBOutlet UILabel *situpDetails;
@property (strong, nonatomic) IBOutlet UILabel *runDetails;
这就是我希望实现的目标(简化):
然而,程序崩溃了
-[UILabel mutableCopyWithZone:]: unrecognized selector sent to instance 0x10b82b920
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel mutableCopyWithZone:]: unrecognized selector sent to instance 0x10b82b920'
运行mutableCopy的那一刻。是否有可能实现我想要的,或者我必须单独编写每个文本更改?您可以为每个标签指定标记0、1和2 然后更改如下方法:
-(void)updateDetailText:(NSArray *)results station:(int)station
{
((UILabel *) [self viewWithTag:station]).text = [NSString stringWithFormat:@"You need %d - %d reps", 10, 50];;
}
或者,您可以在每个案例测试中尝试使用常规分配:
templabel = pushupDetails;
因为您需要引用实际对象,而不是它的副本。
UILabel
不符合NSCopying
类,因此您不能使用copy
或mutableCopy
您必须使用下面的示例
switch (station) {
case 0:
templabel = self.pushupDetails;
break;
case 1:
templabel = self.situpDetails;
break;
case 2:
templabel = self.runDetails;
break;
default:
break;
}
switch (station) {
case 0:
templabel = self.pushupDetails;
break;
case 1:
templabel = self.situpDetails;
break;
case 2:
templabel = self.runDetails;
break;
default:
break;
}