2011-09-23 101 views
0

我在我的應用程序中動態地創建了一些「n」個UIView對象。我可以將這些對象拖放到屏幕上的任何位置& chenge更改它們的一些屬性。現在,我想保存所有這些細節與持久性存儲,以便每當我啓動應用程序嵌套時間,我能夠看到那些已經創建的對象。UIVIew的持久存儲器

那麼最好的解決方案是什麼?

也是他們的任何示例應用程序可用於這種形式,我可以參考?

回答

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

嘿Emptystack感謝這種反應,但是我的要求是,一旦用戶完成了他的任務,他能挽救他的所有drawings.Then,他可以重新再拍新的繪圖,並將其保存(繪圖1,繪製2,3繪圖....)。最後,如果他想要,他可以在分割視圖控制器中看到他所有的積蓄,如果他想讓他能夠編輯和保存他現有的繪圖。 – user930195

+0

現在這在Swift中看起來如何? – Heki