gpt4 book ai didi

objective-c - NSKeyedArchiver 为 UIView 的 subview 返回 nil

转载 作者:行者123 更新时间:2023-11-29 11:12:02 25 4
gpt4 key购买 nike

我有一个 ViewController,我在其中添加了一个名为 DrawingView 的自定义 UIView。我想在此 DrawingView 中添加 UITextView 的动态数量,因此我将 UITextView 子类化为 CustomTextView。在 ViewController 中,我有以下代码将 textview 添加到 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];
}
}

在按钮操作上,我在绘图 View 中添加了 CustomTextView。

currentText = [[TextViewContainer alloc]init];
[drawingView currentText];

现在我想存档此 DrawingView 及其 subview ,即 CustomTextView。所以在 CustomTextView 中我添加了 NSCoding Protocol 并在其中添加了属性

- (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 数组为零。谁能帮帮我。

最佳答案

认为您正在尝试归档 View 本身。

[archiver encodeObject:view forKey:fileName];

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

看看the NSKeyedArchiver ClassReference了解更多详情。另外,请看the link详细解释。谢谢。

关于objective-c - NSKeyedArchiver 为 UIView 的 subview 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11094870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com