开发者

Persistance storage for UIVIew

开发者 https://www.devze.com 2023-04-07 13:13 出处:网络
I am creating dynamically some \"n\" number of UIView Object in my application.I can able to drag, drop these objects to any position in the screen & chenge change their some of property .Now i wa

I am creating dynamically some "n" number of UIView Object in my application.I can able to drag, drop these objects to any position in the screen & chenge change their some of property .Now i want to save all these details with persistance storage ,so that whenever i launched the application nest time, i can able to see those already created object.

So what is th开发者_JAVA技巧e best solution for this?

Also is their any sample application available for this form which i can take reference?


I think you can do it this way.

// 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"];

Later you can get the array from persistence and create the views with the properties.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号