保存 UITextfield 信息时出现问题
当我尝试使用 NSUserdefaults 保存 UITextFields 信息时遇到问题(崩溃)。我知道这与我正在使用的数组有关。这是代码:
- (IBAction)generateNumbers
{
// release and remove everyting from the name array
// [nameArray release];
nameArray = nil;
// reinitilize the name array
nameArray = [[NSMutableArray alloc] init];
// Loop through the textfields to get the names into the nameArray
for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
[nameArray addObject:[[textFieldArray objectAtIndex:textFieldCount] text]];
}
// Randomly sort the names in the array
[nameArray sortUsingFunction:randomSort context:nil];
// Add the random names back into the text fields
for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
[[textFieldArray objectAtIndex:textFieldCount] setText:[nameArray objectAtIndex:textFieldCount]];
}
}
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (arc4random()%3 - 1);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tex1 setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue48"]];
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] setObject:tex1.text forKey:@"storedTextValue48"];
I'm having issues(crashes) when I try to save UITextFields information with NSUserdefaults. I know it has something to do with the array that I'm using. Here is the code:
- (IBAction)generateNumbers
{
// release and remove everyting from the name array
// [nameArray release];
nameArray = nil;
// reinitilize the name array
nameArray = [[NSMutableArray alloc] init];
// Loop through the textfields to get the names into the nameArray
for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
[nameArray addObject:[[textFieldArray objectAtIndex:textFieldCount] text]];
}
// Randomly sort the names in the array
[nameArray sortUsingFunction:randomSort context:nil];
// Add the random names back into the text fields
for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
[[textFieldArray objectAtIndex:textFieldCount] setText:[nameArray objectAtIndex:textFieldCount]];
}
}
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (arc4random()%3 - 1);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tex1 setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue48"]];
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] setObject:tex1.text forKey:@"storedTextValue48"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要忘记使用 synchronize 保存数据时的方法:
Don't forget to use synchronize method when you are saving data: