2012-07-10 12 views
5

私はiOSアプリケーションを作成しています。スピナーを使用してUIAlertViewを表示する必要があります。 時々、私はアラートの中心にスピナーを追加しようとすると何かがうまくいかず、通常この前に別のアラートがあったとき(まさしくルールではありませんが、UIActivityIndi​​catorViewが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]; 

そして、この1つは私の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]; 
} 

このソリューションは毎回動作していますが、どうしたのか本当に気に入らないのです。警報がポップアップしてからスピンナーを1秒半表示することはそれほど邪魔ではありませんが、これを処理するにはより良い方法が必要です。私はiOSの専門家でもありませんし、シニアプログラマでもありませんが、イベントを使用することはできませんか? 「実際にアラートが表示されたら、addSpinner関数に移動する」のような何か?私はこれをどうやって行うのか分かりませんし、Web上で多くの助けを見つけることができませんでした...

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

答えて

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]; 
} 
+0

ありがとうございます。使用していただきありがとうございます。私はあなたの答えを私の "受け入れられた答え"としてマークすることはできません。それは実際に問題を解決するものではありません。 – rdurand

+1

問題はありません:) ..もし役に立ちましたら、ただ投票することができます:) – waheeda

+1

私はあなたの問題を解決するコード、それが助けてくれることを願っています。 – waheeda

2

インジケータがメインスレッドにアラートを却下することも忘れないでください、このコード

[megaAlert dismissWithClickedButtonIndex:0 animated:YES]; 
megaAlert = nil; 
[indicator stopAnimating]; 
+0

ありがとうございます。これは基本的に私がアラートを却下することなく最初にやったことです... UIActivityIndi​​catorの位置に影響を与えると思いますか?数分で動作するかどうかをお知らせします。 – rdurand

+0

UIActivityIndi​​catorの位置は、UIAlertViewが表示される前に設定されているため、実際にはこの問題が発生しているようです。私は警報を却下してこの問題を解決するとは思わない... – rdurand

1

を使用却下するには、この

[[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]; 

をお試しください:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.alert dismissWithClickedButtonIndex:0 animated:YES]; 
}); 
4

基本的に問題がに見えますUIA1の前にUIActivityIndi​​catorを追加するertViewが表示されます。以下のようにインジケータを追加するには、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]; 
} 
+1

遅い答えをありがとう、それは問題を解決する可能性がありますが、私はMBProgressHUDを使用して、もうそれに直面していないよ。 – rdurand

関連する問題