0

对不起,如果我重复这个问题,但作为新人,我仍然需要学习你关于如何回应的规则等等。感谢您的帮助,我做到了这一点:我可以通过 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]];
    }
    }
4

1 回答 1

0

您所写的内容中缺少文件名。

尝试使用以下方式保存数据:

NSString *myFile = @"myFile.plist";
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,  NSUserDomainMask, YES);
NSString *documentsDirectory = [filePaths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:myFile];
[myArray writeToFile:path atomically:YES];
于 2013-06-19T12:22:22.790 回答