views:

48

answers:

1

I can't see any way to copy an NSView and create an identical NSView object. I see google hits about "use an NSData" but I don't understand that.

+4  A: 

To straight up "copy" an NSView, the view has to implement the NSCopying protocol. Unfortunately, NSView does not.

Fortunately, it does implement the NSCoding protocol, which means we can still duplicate a view like:

NSData * archivedView = [NSKeyedArchiver archivedDataWithRootObject:myView];
NSView * myViewCopy = [NSKeyedUnarchiver unarchiveObjectWithData:archivedView];

And voilá! You now have a duplicate of myView.

Dave DeLong
Note that if you subclass, you'll need to implement archiving support. The docs know all.
bbum
In every case that I subclass - even when I don't have data added but only a couple methods?
Nektarios
Nektarios: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmImplementCopy.html
Peter Hosey