하나의 ViewController가 있습니다.이 중 하나에 DrawingView라는 Custom UIView가 하나 추가되었습니다. 이 DrawingView UITextView 동적 번호를 추가 할 그래서 UITextView CustomTextView 같은 클래스 이름으로 서브 클래 싱했습니다. ViewController있는 DrawingView textview 추가하려면 다음 코드가 있습니다.NSKeyedArchiver는 UIView의 하위 뷰에 대해 nil을 반환합니다.
- (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);
}
하지만 서브 뷰 배열이 nil로 표시됩니다. 아무도 나를 도와 줄 수 없어.