ios - 我如何以编程方式关闭 UIAlertController

标签 ios objective-c uialertcontroller

我需要以编程方式关闭我用作“请稍候”消息的 UIAlertController。我可以毫无问题地显示警报,但在解除警报时,50% 的时间它会解除,而另外 50% 的时间则不会,这迫使我重新启动应用程序以继续使用它。任何想法如何以 100% 的一致性解除警报?

//loadingAlert is a UIAlertController declared in the .h file
//present the Alert
loadingAlert = [UIAlertController alertControllerWithTitle:@"Loading..." message:@"Please wait while we fetch locations" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:loadingAlert animated:YES completion:nil];

//parse JSON file
_listOfAcquisitions = nil;
MNSHOW_NETWORK_ACTIVITY(YES);
NSString *WebServiceURL = [NSString stringWithFormat:@"JSON URL", _search];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSDictionary *dictionary = [JSONHelper loadJSONDataFromURL:WebServiceURL];
    dispatch_async(dispatch_get_main_queue(), ^{
        _listOfAcquisitions = [NSMutableArray array];
        for (NSDictionary *oneEntry in dictionary) {
            Acquisitions *acqu = [[Acquisitions alloc] init];

            if([oneEntry objectForKey:@"ADDRESS1"] == (NSString *)[NSNull null]){acqu.ADDRESS1 = @"";}
            else {acqu.ADDRESS1 = [oneEntry objectForKey:@"ADDRESS1"];}

            if([oneEntry objectForKey:@"STATEABBR"] == (NSString *)[NSNull null]){acqu.STATEABBR = @"";}
            else {acqu.STATEABBR = [oneEntry objectForKey:@"STATEABBR"];}

            if([oneEntry objectForKey:@"TOWN"] == (NSString *)[NSNull null]){acqu.TOWN = @"";}
            else {acqu.TOWN = [oneEntry objectForKey:@"TOWN"];}

            if([oneEntry objectForKey:@"ZIPCODE"] == (NSString *)[NSNull null]){acqu.ZIPCODE = @"";}
            else {acqu.ZIPCODE = [oneEntry objectForKey:@"ZIPCODE"];}

            [_listOfAcquisitions addObject:acqu];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            MNSHOW_NETWORK_ACTIVITY(NO);

            [self refreshAnnotations:self];

        });
    });
});
//finally dismiss the alert...
[loadingAlert dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

我一直在学习如何做到这一点。

因此,无论在何处构建警报 Controller ,您都需要将操作按钮添加到默认样式的“确定”或取消样式的“取消”。

UIAlertController *alertController = [UIAlertController 
    alertControllerWithTitle:@"You made a mistake." 
    message:@"Pray we don't alter the alert further" 
    preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okayAction = [UIAlertAction 
    actionWithTitle:@"OK" 
    style:UIAlertActionStyleDefault 
    handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
[alertController addAction:okayAction];
[self presentViewController:alertController animated:YES completion:nil];

还有其他 UIAlertActionStyle 枚举,例如 UIAlertActionStyleCancel 会在其他操作之间放置一个分隔符,UIAlertActionStyleDestructive 会使字体变红但会与其他 UIAlertActions 保持一致。
确保按顺序添加:标准操作(好的,打开相机,照片库)然后取消操作。

还有 preferredStyle:UIAlertControllerStyleActionSheet 用于为用户设置选项。我用它来显示相机和照片库。

此外,对于您的特定解雇操作。它不起作用的原因是因为您试图在后台线程上关闭它。您应该始终在前台线程上关闭或更改 UI。这将在未来导致 NSException/崩溃。

dispatch_async(dispatch_get_main_queue(), ^{
    // dismiss your UIAlertController
});

这就是你应该如何使用你的特定代码来做到这一点:

dispatch_async(dispatch_get_main_queue(), ^{
        MNSHOW_NETWORK_ACTIVITY(NO);

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

您的代码还有其他问题,您应该向其他人寻求帮助。我只是一名初级开发人员,所以我不确定如何正确地做你想做的事,但这应该有助于解雇。
您可能需要查看会显示“请稍候”或“正在加载”的加载轮或 toast 消息。
使用 UIAlertController 来显示加载消息是相当糟糕的品味。

关于ios - 我如何以编程方式关闭 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34799212/

相关文章:

iOS Storyboard - 创建自定义的 UITableViewCell

user-interface - 微调警报 ionic

ios - Swift 4 - SwiftyJson 比较数组并合并找到的值

ios - 如何让 Voiceover 拼出单词中的每个字母?

ios - 发送了无法识别的选择器。 Objective-C 错误

ios - UIAlertController 按钮位置不一致

ios - SWIFT - 如何在显示 UIAlertController 消息后更改它

ios - 计算等距网格上矩形内包含的单元格

iphone - iPhone OS (iOS 4) 上的 Bignum、线性代数和数字信号处理

ios - 使用 AssetPath 创建 NSURL