對不起,如果我重複這個問題,但是新的我仍然必須學習如何響應等規則。 感謝您的幫助,我得到了這一點:我可以通過我的UIView上的AddButton創建10個不同的按鈕。當我再次關閉我的應用程序時,10個按鈕消失。那麼我的用戶被迫重新創建10個按鈕。 如何保存從我的用戶創建的按鈕?保存並重新加載按鈕查看(哪裏是錯誤?)
我會用這種方法更好,因爲如果我要使用其他日期也可以是有用
我具有用戶數據保存到文件成功:是
但是,不要走!
如果我關閉並重新啓動應用程序的按鈕不再出現
哪裏是錯誤?
編輯我的文件的.h以這種方式
{
IBOutlet UIButton * button2;
IBOutlet UIButton * Button1;
IBOutlet UIButton * Button2;
IBOutlet UIButton * Button3;
}
@property (nonatomic, strong) NSArray * myArray;
@property (nonatomic, retain) UIButton * Scenario1;
@property (nonatomic, retain) UIButton * Scenario2;
@property (nonatomic, retain) UIButton * Scenario3;
- (IBAction) Button1: (UIButton *) sender;
- (IBAction) Button2: (UIButton *) sender;
- (IBAction) Button3: (UIButton *) sender;
開文件的.m
@synthesize myArray;
@synthesize Button1;
@synthesize Button2;
@synthesize Button3;
-(void) AddButton: (UIButton *) sender {
CountButton + +;
if (CountButton <11) {
button2 = [UIButton buttonWithType: UIButtonTypeCustom];
button2.frame = CGRectMake (20, 80, 120, 80);
UIImage *ButtonImage = [UIImage imageNamed: @ "image"];
[Button2 setBackgroundImage: ButtonImage forState: UIControlStateNormal]; NSString * buttonTitle = [NSString stringWithFormat: @ "Button% d", CountButton];
[Button2 setTitle: buttonTitle forState: UIControlStateNormal];
NSString *selectorName = [NSString stringWithFormat: @ "% d ButtonAdd:" CountButton];
[Button2 addTarget: self action: NSSelectorFromString (selectorName) forControlEvents: UIControlEventTouchUpInside];
NSArray * = documentDirectories NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString * documentDirectory = [documentDirectories objectAtIndex: 0];
NSArray * myArray = [[NSArray alloc] initWithObjects: Button1, Button2, Button3, nil];
NSString * filePath = [documentDirectory stringByAppendingPathComponent: @ "UserButtons"];
BOOL FileError = [myArray writeToFile: filePath atomically: YES];
NSLog (@ "User date to file successful:% @", (FileError? @ "Yes": @ "No"));
}
}
-(void) viewDidLoad {
[Super viewDidLoad];
NSArray * = documentDirectories NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString * documentDirectory = [documentDirectories objectAtIndex: 0];
NSString * myPath = [documentDirectory stringByAppendingPathComponent: @ "UserButtons"];
NSArray * fileArray = [[NSArray alloc] initWithContentsOfFile: myPath];
if ([fileArray count]> 0)
{
// Place your buttons back into the view.
[Self.view addSubview: [fileArray objectAtIndex: 0]];
}
}
那麼你可以簡單的添加按鈕的排列....而且鑑於檢查做負載如果([addedButtonArray計數]),然後添加所有按鈕從一個一個添加的按鈕數組...如果不是,那麼什麼都不做......每次當你添加按鈕時,只需將它添加到數組中。最後,從應用程序退出/背景只是將其保存在userdefaults – alpha