-->

插入警报视图但不工作(Inserting alert view but not functionin

2019-09-17 00:08发布

我有一个情况我需要提醒用户下次访问该视图控制器是“数据加载”。

我已将此添加到FirstViewController按钮操作:

- (IBAction)showCurl:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Please Wait" message:@"Acquiring data from server" delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil];
    [alert show];
    SecondViewController *sampleView = [[SecondViewController alloc] init];
    [sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentModalViewController:sampleView animated:YES];
}

这是行不通的。 它加载到SecondViewController,只加载SecondViewController后弹出。

所以,我想在SecondViewController本身。 该SecondViewController由这是它要需要一段时间取决于Internet连接以下载的原因在远程服务器中提取数据。 所以我决定加入UIAlertView中的功能:

- (NSMutableArray*)qBlock{
    UIAlertView *alert_initial = [[UIAlertView alloc]initWithTitle:@"Loading" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert_initial show];

    NSURL *url = [NSURL URLWithString:@"http://www.somelink.php"];
    NSError *error;
    NSStringEncoding encoding;
    NSString *response = [[NSString alloc] initWithContentsOfURL:url 
                                                    usedEncoding:&encoding 
                                                           error:&error];
    if (response) {
        const char *convert = [response UTF8String];
        NSString *responseString = [NSString stringWithUTF8String:convert];
        NSMutableArray *sample = [responseString JSONValue];
        return sample;
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Internet Connection cannot be established." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    return NULL;
}

这并不工作过。 而最糟糕的是,我试图关闭网络连接来看看第二警报弹出提醒用户,有没有互联网连接。 第二警报不工作过。

Answer 1:

对于问题的第一部分: show的方法UIAlertView不阻塞当前线程,所以会继续执行,你的行为是正常的。 你必须做的是实现的一个UIAlertViewDelegate的方法和警报的设置delegate财产self 。 因此,当被驳回了警报,你能展现SecondViewController


对于第二部分,如果你有你的qBlock方法在后台线程中执行,然后这是正常的,提醒您不要再显示-你需要证明你的警报在主线程,其中UI运行。 要做到这一点改变你的else用下面的语句:

else
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Internet Connection cannot be established." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show]; 
    });
}

希望这可以帮助。



文章来源: Inserting alert view but not functioning