5

iOSアプリを作成していますが、スピナーを使用してUIAlertViewを表示する必要があります。アラートの中央にスピナーを追加しようとすると、何かがうまくいかないことがあります。通常、このアラートの直前に別のアラートがあった場合です(正確にはルールではありませんが、これが失敗に気づいた方法です)。

スピナーの作成を遅らせることで、このバグを部分的に解決しました。これが私がそれを行う方法です(アラートはメンバーUIAlertViewです):

このコードはviewDidLoadにあります:

alert = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease];

[alert show];

[self performSelector:@selector(addSpinner) withObject:nil afterDelay:0.5f];

そしてこれは私のaddSpinner関数です:

- (void) addSpinner{

    UIActivityIndicatorView *indicator2 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator2.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);

    [indicator2 startAnimating];
    [alert addSubview:indicator2];
    [indicator2 release];
}

このソリューションは毎回機能しますが、私はそれがどのように行われるかが本当に好きではありません。アラートがポップアップしてから0.5秒後にスピナーを表示することはそれほど邪魔になりませんが、これを処理するためのより良い方法があるはずです。私はiOSの専門家でもシニアプログラマーでもありませんが、イベントを使用することはできませんか?「アラートが実際に表示されたら、addSpinner関数に移動します」のようなものですか?私はこれを行う方法がわかりません、そしてウェブ上で多くの助けを見つけることができませんでした...

誰かが私を助けてくれることを願っています!ありがとう。

4

4 に答える 4

5

代わりにhttps://github.com/jdg/MBProgressHUDを使用できます。

アラート内のスピナーの場合、これらの方法を使用できます。

-(void)startLoading
{
    alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [alert addSubview:progress];
    [progress startAnimating];
    [progress release];
    [alert show];
}
-(void)stopLoading
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    [alert release];
}
于 2012-07-10T07:39:48.530 に答える
4

基本的に問題は、UIAlertViewが表示される前にUIActivityIndi​​catorを追加することであるように見えます。UIAlertView Delegateメソッドを使用して、以下のようにインジケーターを追加してみてください。

//AlertView delegate methods
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    indicator.center = CGPointMake(alertView.bounds.size.width/2, alertView.bounds.size.height/3 * 2);

    [indicator startAnimating];
    [alertView addSubview:indicator];
}
于 2013-02-09T11:55:45.313 に答える
2

これを試して

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
megaAlert = [[[UIAlertView alloc] initWithTitle:@"Please wait..."
                                                         message:nil delegate:self cancelButtonTitle:nil
                                               otherButtonTitles: nil] autorelease];
[megaAlert show];
indicator = [[UIActivityIndicatorView alloc]                                                      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            indicator.center = CGPointMake(megaAlert.bounds.size.width / 2,                                            megaAlert.bounds.size.height - 45);
[indicator startAnimating];
[megaAlert addSubview:indicator];
[indicator release];

インジケーターを閉じるには、このコードを使用します

[megaAlert dismissWithClickedButtonIndex:0 animated:YES];
megaAlert = nil;
[indicator stopAnimating];
于 2012-07-10T07:49:52.847 に答える
1

メインスレッドでアラートを閉じることも忘れないでください。

dispatch_async(dispatch_get_main_queue(), ^{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];
});
于 2012-11-12T13:08:45.790 に答える