怎么复制UILabel? [英] How do copy for UILabel?

查看:275
本文介绍了怎么复制UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 IBOutlet UILabel *标签;



我想要这样做

  UILabel * label = [titleLabel copy]; 
label.text = @克隆;
titleLabel.text = @Original;
NSLog(@label:%@,title:%@,label.text,titleLabel.text);

此抛出异常


*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UILabel copyWithZone:]:无法识别的选择器发送到实例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)



解决方案

没有公开的Apple API可以深层复制UILabel。你最好的办法是制作一个帮助方法,复制你关心的所有部分。

   - (UILabel *)deepLabelCopy :( UILabel *)标签{
UILabel * duplicateLabel = [[UILabel alloc] initWithFrame:label.frame];
duplicateLabel.text = label.text;
duplicateLabel.textColor = label.textColor;
//等等......对你的ULabel

返回重要的任何其他内容[duplicateLabel autorelease];
}

如果要在代码库中使用它,可以将其更改为一个静态方法,并将其放在某种实用类中。如果您将类命名为 LabelUtils ,则可以执行以下操作...

  +(UILabel *)deepLabelCopy(UILabel *)标签{
// ... ...
}

将使用调用UILabel * dupLabel = [LabelUtils deepLabelCopy:origLabel];


I have IBOutlet UILabel *label;

and I want to do this

UILabel *label = [titleLabel copy];
label.text = @"Clone";
titleLabel.text = @"Original";
NSLog(@"label : %@, title : %@",label.text,titleLabel.text);

and this throw exception

* 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)

解决方案

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];
}

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 {
    // ...
}

and would be called using UILabel *dupLabel = [LabelUtils deepLabelCopy:origLabel];

这篇关于怎么复制UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆