- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
在 iOS 12 中,NSKeyedArchiver 的初始化程序 init(forWritingWith:) 已被弃用。 Xcode 10 建议用新的初始化程序 init(requiringSecu
您好,我需要将 NSKeyedArchiver 类加载到 C++/CLI 对应项。有没有办法获取 NSKeyedArchiver 的内部格式? 另一种选择是将整个保存和打开代码重写为适用于 Mac 和
我有一个有时会更改的对象列表,并且我希望每当应用程序关闭或移动到后台时都在设备上保留持久缓存。列表中的大多数对象都不会改变,所以我想知道保存列表的最佳方法是什么。我考虑了两个主要选择: 使用 NSKe
所以我实现了这两个函数来在我的应用程序中保存数据: - (NSString*)itemArchivePath { NSArray* documentDirectories = NSSearchP
根据我的理解,完整的对象图是使用以下方式存储的: NSKeyedArchiver archiveRootObject 并且按照保存的顺序检索它,这是正确的吗? 我正在获取一个 NSDictionary
@界面 @interface Patient : NSObject @实现 - (id)initWithCoder:(NSCoder *)decoder{ self.patientPhoto
所以我有一个包含一些自定义类的数组: 带游戏的 NSMutableArray (FirstViewcontroller)- 骰子游戏 (Game.m)-- 带选项的骰子 (Dices.m) 在每个自定
我似乎无法让 NSKeyedArchiver 工作。我有一个主 ViewController,它有一个 NSMutableArray 作为实例变量。该数组由自定义类的多个实例填充。自定义类符合NSCo
因此,我的应用程序查询 Amazon Dynamo DB 数据库并检索几千字节的数据。我希望应用程序做的是第一次下载所有内容,然后每次都下载时间戳以查看它是否具有最新版本的数据。所以我只需要每隔一段时
我正在使用 Objective-C 构建一个应用程序,我想保留数据。我在 NSKeyedArchiver 和核心数据之间犹豫不决。我知道网上有很多关于此的资源(包括 Objective-C best
我在使用 NSKeyedArchiver 时遇到问题。我正在尝试归档一个字典,它又包含自定义对象、专辑和歌曲的数组。 Album 和 Song 都是 NSObject 固有的,并且符合 NSCodin
我的应用使用自定义类作为其数据模型: class Drug: NSObject, NSCoding { // Properties, methods etc... } 我刚刚创建了一个 Tod
我目前正在调查一个据称通过 NSKeyedArchiver 存储的文件解档期间发生的崩溃。崩溃日志包含文件的前 8 个字节四部分(下面我只包括前 2 个)。 Fatal Exception: NSIn
我正在用下面的做IOS App数据备份 NSKeyedArchiver.archiveRootObject() NSKeyedUnarchiver.unarchiveObjectWithFile()
我在使用 NSKeyedArchiver.archivedData 时遇到 unrecognized selector sent to instance 错误。 我试图将解析的 JSON 输出存储到自
我正在尝试使用 NSKeyedArchiver 来“归档”一个简单的字典。键是 String 类型,值是 enum。 以下示例说明了我正在尝试做的事情: import Foundation enum
我是否需要担心在后台线程中使用 archiveRootObject 和 unarchiveObjectWithFile? dispatch_async(dispatch_get_global_queu
注意:Xcode 8 Beta 6 我不确定我遗漏了什么,但出于某种原因,我在 NSKeyedArchiver.archivedData 行上收到以下错误。 'NSInvalidArgumentExc
我有一个希望保存和加载的自定义类。该类包含一个 NSDate、一个 NSString 和一个 NSNumber。我已经在 .h 文件中实现了 NSCoding 协议(protocol)。这是我到目前为
我正在编写一个使用 NSKeyedArchiver 来保存其数据的应用程序。我的文件应该是 ishotTrack.plist 还是 ishotTrack.arch? 最佳答案 使用 NSKeyedAr
我是一名优秀的程序员,十分优秀!