I have a UIView that is linked to a UIViewController via the Interface Builder. Is it possible to duplicate, clone or copy this view so that I can use it more than once?
The following category might not be particularly efficient, but worked for me in one project:
@implementation UIView (OPCloning) - (id) clone { NSData *archivedViewData = [NSKeyedArchiver archivedDataWithRootObject: self]; id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData]; return clone; } @end
I'd not implement -copy or -copyWithZone: as Apple might do so in the future. Note, that not all views implement archiving to the same extent. You'll definitely need to implement the NSCoding methods for custom properties of your NSView subclasses to be cloned (will turn to nil in the cloned view, otherwise). Still easier than writing custom cloning code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With