How do copy for UILabel?(如何为 UILabel 复制?)
问题描述
我有 IBOutlet UILabel *label;
我想这样做
UILabel *label = [titleLabel copy];
label.text = @"Clone";
titleLabel.text = @"Original";
NSLog(@"label : %@, title : %@",label.text,titleLabel.text);
这个抛出异常
* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x6a4a450"* 首先抛出调用栈:(0x126f052 0x1823d0a 0x1270ced 0x11d5f00 0x11d5ce2 0x1271bd9 0x2ed6 0x1270e1a 0x2851 0x28264e 0x1e2a73 0x1e2ce2 0x1e2ea8 0x1e9d9a 0x24af 0x1ba9d6 0x1bb8a6 0x1ca743 0x1cb1f8 0x1beaa9 0x215cfa9 0x12431c5 0x11a8022 0x11a690a 0x11a5db4 0x11a5ccb 0x1bb2a7 0x1bca9b 0x21c2 0x2135)
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x6a4a450' * First throw call stack: (0x126f052 0x1823d0a 0x1270ced 0x11d5f00 0x11d5ce2 0x1271bd9 0x2ed6 0x1270e1a 0x2851 0x28264e 0x1e2a73 0x1e2ce2 0x1e2ea8 0x1e9d9a 0x24af 0x1ba9d6 0x1bb8a6 0x1ca743 0x1cb1f8 0x1beaa9 0x215cfa9 0x12431c5 0x11a8022 0x11a690a 0x11a5db4 0x11a5ccb 0x1bb2a7 0x1bca9b 0x21c2 0x2135)
推荐答案
没有公共的 Apple API 可以深度复制 UILabel.最好的办法是创建一个辅助方法来复制您关心的所有部分.
There is no public Apple API to deep copy a UILabel. Your best bet is to make a helper method which copies all the parts you care about.
- (UILabel *)deepLabelCopy:(UILabel *)label {
UILabel *duplicateLabel = [[UILabel alloc] initWithFrame:label.frame];
duplicateLabel.text = label.text;
duplicateLabel.textColor = label.textColor;
// etc... anything else which is important to your ULabel
return [duplicateLabel autorelease];
}
如果您想在整个代码库中使用它,您可以将其更改为静态方法并将其放入某种实用程序类中.如果您将类命名为 LabelUtils
,您可以执行类似...
If you want to use it all over your code base you can change it to a static method and put it in some sort of utility class. If you named the class LabelUtils
you could do something like...
+ (UILabel *)deepLabelCopy(UILabel *)label {
// ...
}
并且将使用 UILabel *dupLabel = [LabelUtils deepLabelCopy:origLabel];
这篇关于如何为 UILabel 复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 UILabel 复制?


- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01