Is it possible to duplicate a UILabel so that I can add it as a subtitle for multiple UIView?

I know that a view can only be a subview of one supervisor, so that you can duplicate a UILabel, so I don’t need to copy the setup code or write a function to create it?

+4
source share
4 answers

No, the UILabel class does not implement the NSCopying protocol. If you want to add the same view to all of your views, it might be best to select a subclass of your custom view.

+12
source

As @starkhalo said, UILabel not NSCopying . therefore, you cannot use the copy method using UILabel.

Iphone docs for NSObject clearly says -

NSObject itself does not support the NSCopying protocol. Subclasses must support the protocol and implement the copyWithZone: method.

0
source

You can configure a separate XIB for this shortcut only and call [[NSBundle mainBundle] loadNibNamed:@"LabelNib" owner:self options:nil]; Configure the file owner of this nib to the delegate class and establish the correct connections. Now you can add a label to your view as many times as necessary and change each of them (for example, using a for loop).

-1
source

The copy method is available to everyone that inherits from NSObject , so my first port of call will just try

 UILabel *second = [first copy]; 

However, this may not work 100%, as expected, some UILabel properties cannot be copied as you want. If not, can you try using NSCoding methods - encode an object and then decode it into a new object?

It might be easier to do manually, though :)

Sam

-thirteen
source

Source: https://habr.com/ru/post/1305329/


All Articles