2012-05-03 22 views
0

我有一種情況,我需要提醒用戶訪問的下一個視圖控制器是「數據加載」。插入警報視圖但不起作用

我已將此添加到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; 
} 

這並不工作過。最後,我試圖關閉互聯網連接,看看是否有第二個警報彈出來提醒用戶沒有互聯網連接。第二次警報不起作用。

+0

爲什麼不使用'UIActivityIndi​​catorView'來代替?它更加便於用戶使用。 – sooper

+0

你可以指導我如何? –

回答

1

對於問題的第一部分:的show方法不會阻止當前線程,因此執行會繼續,並且您的行爲是預期的。您需要做的是執行UIAlertViewDelegate的方法之一,並將警報的delegate屬性設置爲self。因此,當警報解除時,您可以顯示您的SecondViewController


對於第二部分,如果你有那麼這是正常的,提醒您不要再顯示您的qBlock方法在後臺線程中執行的 - 你需要證明你的警報在主線程,用戶界面運行的位置。要做到這一點改變

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]; 
    }); 
} 

希望這會有所幫助。

+0

我在哪裏實施第一種方法?在FirstViewController的SecondViewController中? –

+0

在FirstViewController中。 show方法應該顯示UIAlertView,並且當警報視圖被解除時(您可以通過實現一些UIAlertViewDelegate方法並將警報的委託設置爲self來捕獲),則初始化並呈現SecondViewController。 – graver

+0

我可以實現當我設置取消按鈕。但是我想要的是沒有取消按鈕。我希望警報彈出來顯示tt –