0

次にアクセスするView Controllerが「Data Loading」であることをユーザーに警告する必要がある状況があります。

これを 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;
}

これもうまくいきません。さらに、インターネット接続をオフにして、インターネット接続がないことをユーザーに警告する 2 番目のアラートが表示されるかどうかを確認しました。2 番目のアラートも機能しません。

4

1 に答える 1

1

質問の最初の部分:のshowメソッドはUIAlertView現在のスレッドをブロックしないため、実行は続行され、期待される動作が期待されます。あなたがしなければならないことは、UIAlertViewDelegateのメソッドの1つを実装し、アラートのdelegateプロパティをに設定することselfです。したがって、アラートが却下されたときに、を表示できますSecondViewController


2番目の部分では、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]; 
    });
}

お役に立てれば。

于 2012-05-03T11:57:34.830 に答える