2012-06-19 4 views
1

하나의 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로 표시됩니다. 아무도 나를 도와 줄 수 없어.

답변

2

보기 자체를 보관하려고하는 것 같습니다.

[archiver encodeObject:view forKey:fileName]; 

내가 잘못 생각합니다. UI 구성 요소를 보관할 수는 없지만 모델 만 보관할 수는 있습니다. 예를 들어, 특정 뷰의 크기, 색상 및 배경 이미지를 포함하여 모델을 데이터베이스에 저장할 수 있으며 뷰 자체를 데이터베이스에 저장할 수 없습니다. 마찬가지로 아카이브의 경우 UI 구성 요소 자체가 아닌 데이터 (즉, 배열, 이미지 등)를 아카이브해야합니다.

자세한 내용은 the NSKeyedArchiver ClassReference을 참조하십시오. 자세한 설명은 the link을 참조하십시오. 감사합니다. .