tags:

views:

147

answers:

3

I know that a view can only be a subview of one superview, so is it possible to duplicate the UILabel so I don't have to copy the set-up code or write a function to create it?

+2  A: 

No, the UILabel class doesn't implement the NSCopying protocol. If you want to add the same view to all your views, maybe subclassing your custom view would be the best way to go.

A: 

The copy method is available by anything that inherits from NSObject so my first port of call would just be to try

UILabel *second = [first copy];

However, this might not work 100% as expected, some UILabel propertiesmight not get copied how you want. If not, you could try using NSCoding methods - encode the object and then decode it into a new object?

It might just be easier to do it by hand though ;)

Sam

deanWombourne
A: 

As said by @starkhalo, UILabel does not conform to NSCopying. so you can not use copy method with UILabel.

Iphone docs for NSObject clearly says -

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

saurabh