Reputation: 3249
I have one ViewController, in this i have added one Custom UIView named as DrawingView. I want to add dynamic number of UITextView in this DrawingView so i subclassed UITextView with class name as CustomTextView. In ViewController i have following code to add textview to DrawingView.
- (void)viewDidLoad
{
[super viewDidLoad];
DrawingView * newDrawingView = [[DrawingView alloc]init];
self.drawingView = newDrawingView ;
}
-(void)setDrawingView:(DrawingView *)_drawingView
{
if (drawingView != _drawingView)
{
[drawingView removeFromSuperview];
drawingView = _drawingView;
drawingView.customDelegate = self;
drawingView.frame = scrollView.bounds;
drawingView.layer.cornerRadius = 8;
drawingView.backgroundColor = [UIColor whiteColor];
drawingView.clipsToBounds = YES;
[self.view addSubview:drawingView];
}
}
On button action i add the CustomTextView in the drawing view.
currentText = [[TextViewContainer alloc]init];
[drawingView currentText];
Now I want to archive this DrawingView along with its subviews i.e the CustomTextView. So in CustomTextView i added NSCoding Protocol and added properties in it
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
self.layer.borderWidth = [aDecoder decodeFloatForKey: @"borderWidth"] ;
}
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeFloat:self.layer.borderWidth forKey:@"borderWidth"];
}
there are other custom properties in the above methods. For archiving i am using following methods.
- (NSData *)dataForView:(UIView *)view:(NSString *)fileName
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:view forKey:fileName];
[archiver finishEncoding];
return (id)data;
}
-(void)saveChangesInFile :(NSString *)fileName
{
NSData *data = [self dataForView:drawingView:fileName];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataPath = [docsDirectory stringByAppendingPathComponent:@"/.hidden"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
}
NSString *pathes = [dataPath stringByAppendingPathComponent:fileName];
[data writeToFile:pathes atomically:YES];
}
- (UIView *)viewForData:(NSData *)data:(NSString*)key
{
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
UIView *view = [unarchiver decodeObjectForKey:key];
[unarchiver finishDecoding];
return view;
}
-(void)openSavedFileWithName:(NSString*)fileName{
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataPath = [docsDirectory stringByAppendingPathComponent:@"/.hidden"];
NSString *filepath = [dataPath stringByAppendingFormat:@"/%@",fileName];
NSData *data = [NSData dataWithContentsOfFile:filepath];
if (data)
{
UIView *newDrawView = [self viewForData:data:fileName];
}
NSLog(@"neew :%@",newDrawView.subviews);
self.drawingView = (DrawingView *)newDrawView ;
NSLog(@"self.drawingView :%@",self.drawingView.subviews);
}
But I am getting subview array as nil. Can anybody help me please.
Upvotes: 1
Views: 1002
Reputation: 845
Think you are trying to archive a view itself.
[archiver encodeObject:view forKey:fileName];
I think its wrong. You cannot Archive any UI components but you can only archive the model. For example you can save the model (including the size, color and background images of the particular view) to the database and cannot save the view itself to the database. Likewise for archiving, u need to archive its data(viz, array, image, etc.) and not the UI components themselves.
Have a look at the NSKeyedArchiver ClassReference for the further details. Also, please look at the link for detailed explaination. Thanks.
Upvotes: 2