如果我在字段中没有任何文本的情况下保存文本,我在Parse.com中收到此错误消息:更新失败 - 操作无法完成(解析错误122.)如果我按OK然后尝试使用取消(按钮项)取消视图应用程序崩溃。我认为Parse.com上的有效文件名必须包含至少1个字符。也许我可以做些什么来阻止用户在不输入文本时保存?有任何想法吗?
这是我的代码:
- (IBAction)save:(id)sender {
// Create PFObject with profile information
PFUser *profile = [PFUser currentUser];
[profile setObject:nameTextField.text forKey:@"name"];
[profile setObject:titleTextField.text forKey:@"title"];
[profile setObject:locationTextField.text forKey:@"location"];
// Profile image
NSData *imageData = UIImageJPEGRepresentation(profileImageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:@"%@", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:@"profileimageFile"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Updating";
[hud show:YES];
// Upload profile to Parse
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:@"Profile Information" message:@"Fill in atleast one field" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Successfully updated profile" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"profile" sender:self];
}
}];
}
- (IBAction)Cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"profile" sender:self];
}
答案 0 :(得分:1)
测试你的单字母理论是否属实。变化:
NSString *filename = [NSString stringWithFormat:@"%@", nameTextField.text];
要:
NSString *filename = [NSString stringWithFormat:@"file%@", nameTextField.text];
如果它是空白的话,可以避免它。所以这个:
NSString *filename = [NSString stringWithFormat:@"%@", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:@"profileimageFile"];
变为:
if (nameTextField.text) {
NSString *filename = [NSString stringWithFormat:@"%@", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:@"profileimageFile"];
}
另外,这是什么:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
它似乎与任何东西无关?
您可以快速连续调用两次,然后在文件保存后再次调用。方法中是否存在必要的重复调用?
[hud hide:YES];
您的if语句似乎与任何内容无关:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
我假设你想要:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0) {
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:@"Profile Information" message:@"Fill in atleast one field" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Successfully updated profile" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:@"profile" sender:self];
}
}];
}