objective-c - 插入警报 View 但不起作用

标签 objective-c ios xcode ios5.1

我遇到一种情况,我需要提醒用户下一个访问的 View Controller 是“数据加载”。

我将其添加到 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 从远程服务器提取数据,这就是根据互联网连接需要一段时间才能下载的原因。所以我决定在函数中添加 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;
}

这也行不通。最重要的是,我尝试关闭互联网连接,看看是否会弹出第二个警报来提醒用户没有互联网连接。第二个警报也不起作用。

最佳答案

对于问题的第一部分:UIAlertViewshow 方法不会阻塞当前线程,因此执行会继续,并且您的行为是预期的。您所要做的就是实现 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]; 
    });
}

希望这有帮助。

关于objective-c - 插入警报 View 但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10430606/

相关文章:

ios - 拒绝时显示麦克风许可

android - 无需用户查看源代码即可发送 iPhone 应用程序

ios - Xcode IB : UIButton hidden but have still buttons space

ios - Xcode 13.3 警告 : "' self' refers to the method '{object}.self' , 这可能是意外的

ios - 通过在另一个 ViewController 中按下按钮/在另一个 ViewController 中引用 UITableView 创建 UITableViewCell

ios - 既然我们有 [NSData dataWithContentsOfURL :], 为什么我们要使用 [NSURLConnection sendSynchronousRequest :returningResponse:error:]?

ios - 初始化共享的 NSUserDefaults Swift

ios - 在iOS应用上将美国城市居中

objective-c - 如何打开 NSWindow 并选中窗口并使其处于焦点状态?

ios - UILabel - 设置旋转框架