【问题标题】:Persistance storage for UIVIewUIVIew 的持久存储
【发布时间】:2011-09-23 15:04:04
【问题描述】:

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

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

还有他们的任何示例应用程序可用于此表单,我可以参考吗?

【问题讨论】:

    标签: ios ipad storage datapersistance


    【解决方案1】:

    我认为你可以这样做。

    // 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
    }
    

    【讨论】:

    • 嘿 Emptystack 感谢您的回复,但我的要求是一旦用户完成任务,他可以保存所有图纸。然后他可以再次制作另一张新图纸并保存(图纸 1,图纸 2,图 3....)。最后,如果他愿意,他可以在 splitview 控制器中看到他所有的积蓄,如果他愿意,他可以编辑和保存他现有的绘图。
    • 现在在 Swift 中会是什么样子?
    猜你喜欢
    • 2012-02-02
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 2015-04-06
    • 2013-04-27
    • 1970-01-01
    相关资源
    最近更新 更多