У меня есть один ViewController, в этом я добавил один пользовательский UIView, названный DrawingView. Я хочу добавить динамическое число UITextView в этом DrawingView, поэтому я подклассифицировал UITextView с именем класса как CustomTextView. В ViewController у меня есть следующий код для добавления textview в DrawingView.NSKeyedArchiver возвращает nil для подпрограмм UIView
- (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];
}
}
На действие кнопки Я добавляю CustomTextView в виде чертежа.
currentText = [[TextViewContainer alloc]init];
[drawingView currentText];
Теперь я хочу архивировать этот DrawingView вместе с его подзонами, а именно CustomTextView. Так CustomTextView я добавил NSCoding протокол и добавил свойства в нем
- (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"];
}
существуют и другие пользовательские свойства в указанных выше способов. Для архивирования я использую следующие методы.
- (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);
}
Но я получаю subview массив как ноль. Может кто-нибудь мне помочь.