iOS- Saving View Properties
This may seem like a weird question but thought I would ask here before I spend a couple hours trying to implement it. In my app, I am saving view properties (number of sub views etc) to an sqlite3 database. While reading the data back, I though 'Would it not be so much easier to just store the view in the database?'
So, using something like a blob type do you think it would be possible?
- Bind blob to insert
- view = sqlite3_column_blob(statement, 0); to retrieve
Does this sound like it could work? My biggest concern is knowing the size of the view on insert and select. Is something like this possible or is it not documented because it is too ridiculous?
and then getting it out
UIView conforms to NSCoding, which means you can serialize and deserialize it. This is exactly how nib files work. First, see the Archives and Serializations Programming Guide.
One easy way to serialize a UIView is like this:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:view]
You can then write out data in whatever way is convenient.
To deserialize the view, use this:
UIView *view = [NSKeyedUnarchiver unarchiveObjectWithData:data];
This will make a new copy of the entire view hierarchy.
Note that UIView only serializes its own properties. If you subclass UIView and want to serialize additional properties, you will need to override encodeWithCoder: and initWithCoder: to add your properties. This is detailed in Encoding and Decoding Objects.