1

我正在我的应用程序中动态创建一些“n”个 UIView 对象。我可以将这些对象拖放到屏幕中的任何位置并更改它们的一些属性。现在我想用持久性存储保存所有这些细节,这样每当我启动应用程序嵌套时间时,我就可以看到那些已经创建的对象。

那么什么是最好的解决方案呢?

他们是否有任何可用于此表格的示例应用程序,我可以参考?

4

1 回答 1

0

我认为你可以这样做。

// Create an array to store the properties
NSMutableArray *viewProperties = [NSMutableArray array]; 

// Loop through all the views
for (UIView *view in views) {

    // Create a dictionary for each view
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    // Store the properties in the dictionary
    [dict setValue:NSStringFromCGRect(view.frame) forKey:@"ViewFrame"];
    ...

    // Add the dictionary to the array
    [viewProperties addObject:dict];
}

// Finally add the array to persistence
[userDefaults setValue:viewProperties forKey:@"ViewProperties"];

稍后您可以从持久性中获取数组并使用属性创建视图。

NSMutableArray *viewProperties = [userDefaults valueForKey:@"ViewProperties"]; 

for (NSDictionary *dict in viewProperties) {

    UIView *view = [[UIView alloc] init];
    NSString *frameAsString = [dict valueForKey:@"ViewFrame"];
    view.frame = CGRectFromString(frameAsString);
    // Get other properties from dictionary and set it to view
}
于 2011-09-23T06:37:40.607 回答