使用NSMutableData和NSKeyedArchiver保存UIObject

时间:2012-04-01 17:59:58

标签: ios xcode

我正在使用故事板构建应用程序。我有一个视图控制器,用户有一组工具(UILabels,图像...),并可以通过单击特定按钮添加这些项目。当我退出视图控制器或关闭应用程序虽然所有数据都丢失了。所以我试着用这种方式设置一个保存按钮:

  - (void)viewDidLoad {


[super viewDidLoad];

UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Camera"
                                 style:UIBarButtonItemStyleBordered
                                 target:self
                                 action:@selector(useCamera:)];

UIBarButtonItem *cameraRollButton = [[UIBarButtonItem alloc] 
                                     initWithTitle:@"Camera Roll"
                                     style:UIBarButtonItemStyleBordered
                                     target:self
                                     action:@selector(useCameraRoll:)];
NSArray *items = [NSArray arrayWithObjects: cameraButton,
                  cameraRollButton, nil];
[toolbar setItems:items animated:NO];

mouseMoved = 0;

   array = [[NSMutableArray alloc]init];
array2 = [[NSMutableArray alloc]init];
  array3 = [[NSMutableArray alloc]init];

    NSFileManager *filemgr; NSString *docsDir; NSArray *dirPaths;
    filemgr = [NSFileManager defaultManager];
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(
                                                   NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    // Build the path to the data file
    dataFilePath = [[NSString alloc] initWithString: [docsDir
                                                      stringByAppendingPathComponent: @"data.archive"]];
    // Check if the file already exists
    if ([filemgr fileExistsAtPath: dataFilePath]) {

        dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile: dataFilePath];
        array = [dataArray objectAtIndex:0]; array2 = [dataArray objectAtIndex:1]; array3 = [dataArray objectAtIndex:2];


}}


-(void)saveData{

    NSMutableArray *contactArray;
    contactArray = [[NSMutableArray alloc] init]; [contactArray addObjectsFromArray:array]; [contactArray addObjectsFromArray:array2]; [contactArray addObjectsFromArray:array3]; [NSKeyedArchiver archiveRootObject:
                                                                                                                                                     contactArray toFile:dataFilePath];
[self.view addSubview: image1];
        [self.view addSubview: text1];
    [self.view addSubview: label1];

}

当我按下按钮时,您可以看到代码是触发器(NSLog),但没有任何反应。在上面的示例中,当我单击“保存”时,应该保存用户创建的UILabel(alloc),当应用程序再次打开时,unarchive按钮应该将标签准确放置在同一位置。我甚至必须为UIImageViews和UITextFields做。

由于我在数组中添加了所有用户创建的标签...,我想将整个对象保存在数组中。上面的代码不起作用。我无法理解为什么。这就是我创建数组和对象的方式(在.h中我添加了NSMutableArray *数组;)

CGRect labelFrame = CGRectMake( 400, 100, 100, 30);

label1 = [[UILabel alloc] initWithFrame: labelFrame];
[label1 setText: @"Untitled"];
[label1 setTextColor: [UIColor orangeColor]];
[self.view addSubview:label1];
label1.backgroundColor = [UIColor clearColor];
     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];

     [label1 addGestureRecognizer:pinchGesture];

if (array == nil) array = [[NSMutableArray alloc]init];
[array addObject:label1];

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

尝试记录加载的对象,您应该看到它们实际已加载。您当然没有看到它们,因为您没有将它们添加到视图层次结构中。从字典中获取对象后尝试添加这些行:

[self.view addSubview: label1];
[self.view addSubview: text1];
[self.view addSubview: image1];

这可能有用,但这不是一个好主意。您真的应该阅读面向对象编程和模型/视图/控制器(MVC)设计模式的一些介绍性材料。