在将数据上传到Parse.com时,UIAlertView似乎冻结了应用程序

时间:2014-07-27 01:08:14

标签: ios xcode parse-platform uialertview ibaction

您好我正在使用解析来帮助我的iOS应用程序的后端,我创建了一个按钮,可以在我的文本字段中保存文本,并在单击按钮时将其上传到云端。

按钮的代码和弹出的提醒如下

- (IBAction)savebutton:(id)sender {
// Create PFObject with recipe information
PFObject *recipe = [PFObject objectWithClassName:@"GolfTimes"];
[recipe setObject:_nameTextField.text forKey:@"Name"];
[recipe setObject:_prepTimeTextField.text forKey:@"MemberNumber"];

[recipe setObject:_NumberOfGolfers.text forKey:@"NumberOfGolfers"];

[recipe setObject:_RequestedTime.text forKey:@"RequestedTime"];


NSArray *ingredients = [_ingredientsTextField.text componentsSeparatedByString: @","];
[recipe setObject:ingredients forKey:@"Telephone"];

// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @"Sending...";
[hud show:YES];

// Upload recipe to Parse
[recipe saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    [hud hide:NO];

    if (!error) {
        // Show success message
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Request Sent" message:@"We will get back to you with confirmation." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

        // Notify table view to reload the recipes from Parse cloud
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTable" object:self];

        // Dismiss the controller
        [self dismissViewControllerAnimated:YES completion:nil];

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

    }

}];}

虽然数据确实正确发送并且视图控制器确实关闭了,但警报似乎冻结了应用程序,我无法在大多数时间点击“确定”。当我点击“确定”按钮时,应用程序模拟器关闭。

有任何建议或帮助吗?

1 个答案:

答案 0 :(得分:3)

我相信你需要将UIAlertView发送到主线程,如果你想要显示它们:编辑:你不能让UIAlertView的代表在UIViewController你刚刚被解雇了!这就是它崩溃的原因;将委托设置为:nil应该解决这个问题。

dispatch_async(dispatch_get_main_queue(),^
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Request Sent" message:@"We will get back to you with confirmation." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];   

    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTable" object:self];

    [self dismissViewControllerAnimated:YES completion:nil];
});