2012-06-19 75 views
1

我有一个ViewController,在这里我添加了一个名为DrawingView的自定义UIView。我想在这个DrawingView中添加动态数量的UITextView,所以我将类名称作为CustomTextView分类为UITextView。在ViewController中,我有以下代码将TextView添加到DrawingView。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); 
} 

但我得到的子视图数组为零。请有人能帮助我吗?

回答

2

认为您正在尝试存档视图本身。

[archiver encodeObject:view forKey:fileName]; 

我觉得它错了。您无法归档任何UI组件,但只能归档模型。 例如,您可以将模型(包括特定视图的大小,颜色和背景图像)保存到数据库,并且不能将视图本身保存到数据库。同样对于归档,您需要归档其数据(即数组,图像等),而不是UI组件本身。

查看the NSKeyedArchiver ClassReference了解更多详情。 另请参阅the link了解详细说明。 谢谢。