简体   繁体   English

从NSData恢复UIView

[英]Restore UIView from NSData

I want to store a UIView in NSData and later on recover the UIView from the stored NSData. 我想在NSData中存储UIView,然后从存储的NSData中恢复UIView。 As far as I can see in debugger, it works, but I can't see the UIView after restoring. 据我所知,在调试器中,它可以工作,但在恢复后我看不到UIView。 This is my code: 这是我的代码:

- (IBAction)selStore:(id)sender {
    dImageView = [NSKeyedArchiver archivedDataWithRootObject:ivDemo];
}

- (IBAction)selDelete:(id)sender {
    NSArray *viewsToRemove = [ivDemo subviews];
    for (UIView *v in viewsToRemove) {
        [v removeFromSuperview];
    }
}

- (IBAction)selRestore:(id)sender {
    ivDemo = [NSKeyedUnarchiver unarchiveObjectWithData:dImageView];
    [ivDemo setNeedsDisplay];
}

Here the results from the debugger after storing the UIView in NSData dImageView 这里是在NSData dImageView中存储UIView后调试器的结果 在此输入图像描述

In _subviewCache are the two UIImageView I added to the UIView. 在_subviewCache中是我添加到UIView的两个UIImageView。

Here the results from the debugger after deleting the subviews in UIView: 这里是在删除UIView中的子视图后调试器的结果: 在此输入图像描述

No _subviewCache are there 没有_subviewCache

Finally I restore the UIView from NSData 最后,我从NSData恢复UIView 在此输入图像描述

the _subviewCache shows the two UIImageViews _subviewCache显示了两个UIImageViews

BUT, the UIView is not refreshed on iPad screen. 但是,UIView在iPad屏幕上没有刷新。 Any idea what I am missing? 知道我错过了什么吗?

I was really curious to see if this was possible (although I can't think of a reason when I'd use it). 我真的很好奇,看看这是否可能(虽然我不能想到我使用它的原因)。

I explored this in Swift, but there should be no difference between Objective-C and Swift in this case. 我在Swift中对此进行了探讨,但在这种情况下,Objective-C和Swift之间应该没有区别。

I found that I could archive/unarchive a single view fine, but as soon as I tried to archive a view with subviews that it acted unexpectedly. 我发现我可以将单个视图归档/取消归档,但是一旦我尝试使用子视图归档视图,它就会意外地进行操作。

The best I could do was to archive an array of views, but again they could only be siblings rather than a nested view hierarchy. 我能做的最好的事情是归档一组视图,但同样它们只能是兄弟姐妹而不是嵌套的视图层次结构。

You'd probably have more luck to create some data model that represented your views, archive the model, and then re-create the views using your model. 您可能更幸运的是创建一些代表您的视图的数据模型,归档模型,然后使用您的模型重新创建视图。

import UIKit

class ViewController: UIViewController {

    var archiver:NSData?

    @IBOutlet weak var containerView: UIView!

    @IBAction func toggle(sender: UIButton) {

        if let data = self.archiver {
            if let views = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [UIView] {

                for view in views {
                    self.containerView.addSubview(view)
                }

                self.archiver = nil
            }
        }
        else {
            let views = self.containerView.subviews

            for view in views {
                view.removeFromSuperview()
            }

            self.archiver = NSKeyedArchiver.archivedDataWithRootObject(views)
        }
    }
}

you can use encodeWithCoder: to get a serialized object, and initWithCoder: to reconstruct a imageview or any view for restoration. 您可以使用encodeWithCoder:获取序列化对象,并使用initWithCoder:重建imageview或任何视图以进行恢复。 Please go through this doc: Serializations Programming Guide for Cocoa . 请仔细阅读以下文档: Cocoa序列化编程指南

eg: 例如:

- (void)encodeWithCoder:(NSCoder *)coder {

    if ([coder isKindOfClass:[NSKeyedArchiver class]]) {
        [coder encodeObject:self.user.firstName forKey:kFirstName];
        [coder encodeObject:self.user.dateOfBirth forKey:kDateOfBirth];
        [coder encodeFloat:self.user.imageView forKey:kUserImageView];
    }
    else {
        [NSException raise:NSInvalidArchiveOperationException format:@"Only supports NSKeyedArchiver coders"];
    }
}

- (void)decodeWithCoder:(NSDecoder *)decoder {



if ([decoder isKindOfClass:[NSKeyedUnarchiver class]]) {
        NSString *userName = [decoder decodeObjectForKey:kFirstName];
        NSDate *dateOfBirth = [decoder decodeObjectForKey:kDateOfBirth];
        UIImageView *userImageView = [decoder decodeObjectForKey:kUserImageView];
    }
    else {
        [NSException raise:NSInvalidArchiveOperationException format:@"Only supports NSKeyedUnarchiver decoders"];
    }
    }

- (NSData *)dataForObject:(User *)userProfile {
  NSMutableData *archivingData = [NSMutableData data];
  NSKeyedArchiver  *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:archivingData];
  [archiver encodeObject:userProfile forKey:@"userProfile"];
  [archiver finishEncoding];
  archiver = nil;

  return (id)data;
}

- (User *)userProfileForData:(NSData *)data {
  NSKeyedUnarchiver  *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

  User *userProfile = [unarchiver decodeObjectForKey:@"userProfile"];
  [unarchiver finishDecoding];
  unarchiver = nil;

  return view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM