How to serialize a UIView?

Is it possible to serialize a UIView object? If so, how can I do this?

+6
objective-c serialization iphone uiview
source share
1 answer

UIView implements the NSCoding protocol, so you can use encodeWithCoder: to get the serialized view and initWithCoder: to restore the UIView from that view. For details, see Serialization Programming Guide for Cocoa .

Here is a brief example of how to do this:

 - (NSData *)dataForView:(UIView *)view { NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:view forKey:@"view"]; [archiver finishEncoding]; [archiver release]; return (id)data; } - (UIView *)viewForData:(NSData *)data { NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; UIView *view = [unarchiver decodeObjectForKey:@"view"]; [unarchiver finishDecoding]; [unarchiver release]; return view; } 
+11
source share

All Articles