Iphone 视图上的保存和重新加载按钮(错误在哪里?)
对不起,如果我重复这个问题,但是作为一个新手,我仍然需要学习你关于如何回应的规则等等。 多亏了你的帮助,我达到了这一点:我可以通过UIView上的AddButton创建10个不同的按钮。当我再次关闭应用程序时,10个按钮消失了。然后我的用户被迫重新创建10个按钮。 如何保存从用户创建的按钮 我会更好地使用这个方法,因为如果我必须使用其他日期,它会很有用 我有要归档的用户数据成功:是 但是不要去 如果我关闭并重新启动应用程序,按钮不会再次出现 错误在哪里 用这种方式编辑我的文件.hIphone 视图上的保存和重新加载按钮(错误在哪里?),iphone,ios,Iphone,Ios,对不起,如果我重复这个问题,但是作为一个新手,我仍然需要学习你关于如何回应的规则等等。 多亏了你的帮助,我达到了这一点:我可以通过UIView上的AddButton创建10个不同的按钮。当我再次关闭应用程序时,10个按钮消失了。然后我的用户被迫重新创建10个按钮。 如何保存从用户创建的按钮 我会更好地使用这个方法,因为如果我必须使用其他日期,它会很有用 我有要归档的用户数据成功:是 但是不要去 如果我关闭并重新启动应用程序,按钮不会再次出现 错误在哪里 用这种方式编辑我的文件.h { I
{
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;
存档
@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]];
}
}
@synthesis-myArray;
@1;
@2;
@合成按钮3;
-(无效)添加按钮:(UIButton*)发送器{
CountButton++;
如果(计数按钮0)
{
//将按钮放回视图中。
[Self.view addSubview:[fileArray objectAtIndex: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];
好的,你可以简单地获取一个添加按钮的数组。。。。并且签入视图确实加载了if([addedButtonArray count]),然后从添加的按钮数组中逐个添加所有按钮。。。如果没有,那就什么也不做。。。每次添加按钮后,只需将其添加到数组中即可。最后,在退出/退出应用程序之前,只需将其保存在userdefaultsNP中,我不知道如何制作?但可能没有我可以帮助的?想要一个心地善良的开发人员前4行创建了文件“myFile.plist”的路径。最后一行允许您在其中写入内容(在本例中为数组)。您可以通过以下方式恢复此同一文件中的数据:
myArray=[NSArray arraywhithcontentsoffile:path]代码>