2011-06-22 16 views
0

基本的なログインログアウト機能を備えたこのiPhoneアプリケーションを開発しています。以下のようなログアウトアクションを実装する必要があります。アラートが表示されない - (void)alertView:clickedButtonAtIndex:method

  1. まずUIAlertView
  2. を使用して、ユーザーの確認を得る、ログアウト処理がサーバ

で扱われている間、サーバーロジックがまだ実装されていない、ログアウト進行中のアラートボックスを表示しますしかし、この2番目の進行中の警告ボックスを表示するときに問題に直面しています。問題は、アラートボックスが表示されず、代わりにメインスレッドだけがスリープ状態になり、ルートビューコントローラに移動することです。誰も私にこの問題を助けることができますか?

私のコードを以下に示します。

ログアウトアクション:

-(IBAction)logOut 
{ 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really Want to Log Out?" 
        message:@"If you continue you will be redirected to Home page, 
        where you have to sign in again. \n\nContinue?" 
        delegate:self cancelButtonTitle:@"Cancel" 
        otherButtonTitles:@"Continue", nil]; 

[alert show]; 

[alert release]; 

} 

次のように(ユーザーがクリックしたボタン)、ユーザの入力が処理されます...

-(void)alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     [NSThread detachNewThreadSelector:@selector(dataProcessor) 
        toTarget:self withObject:nil]; 
     [NSThread sleepForTimeInterval:3]; 
     [self.navigationController popToRootViewControllerAnimated:YES]; 
    } else { 
     // do nothing 
    } 
} 

dataProcessor()は、次のように実装されています。

-(void)dataProcessor { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Signing out...\n\nPlease Wait..." 
          message:nil delegate:self cancelButtonTitle:nil 
          otherButtonTitles: nil] autorelease]; 

    [alert show]; 

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

    indicator.center = CGPointMake(alert.bounds.size.width/2, 
            alert.bounds.size.height - 50); 
    [indicator startAnimating]; 
    [alert addSubview:indicator]; 
    [indicator release]; 


    [alert dismissWithClickedButtonIndex:0 animated:YES]; 

    [pool release]; 

} 

この手法を使用すると、このアラートはUIActivityIndi​​catorで表示されません。何が起きるかは、ウェルカムビューコントローラがブラックアウト(私は[NSThread sleepForTimeInterval:3]のために考えられます)となり、ルートビューコントローラに戻ります。

また、この記事で説明したアラート付きのタグを使用しようとしました。 Show Alert in clickedButtonAtIndex?しかし、まだ私はこれを動作させることができません。

誰でもお手伝いできますか?前もって感謝します。

答えて

2

ここにはかなりのものがあります。

  1. メインスレッド以外のスレッドでUIKit要素を処理する。
  2. アラートを作成して表示し、直ちに解除します。もちろん、コンピュータ時代にはこれはかなり速く起こらなければなりません。だからあなたはそれを見ていないでしょう。
  3. UIをブロックしています。 (もちろん、これはあなたが望むように見えるものです。)

おそらくこの、dataProcessor

-(void)alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     [self dataProcessor]; 
    } else { 
     // do nothing 
    } 
} 

以降、

-(void)dataProcessor { 
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Signing out...\n\nPlease Wait..." 
          message:nil delegate:self cancelButtonTitle:nil 
          otherButtonTitles: nil] autorelease]; 

    [alert show]; 

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

    indicator.center = CGPointMake(alert.bounds.size.width/2, 
            alert.bounds.size.height - 50); 
    [indicator startAnimating]; 
    [alert addSubview:indicator]; 
    [indicator release]; 

    [self performSelector:@selector(dismissAndPop:) withObject:alert afterDelay:3.0f]; 
} 

のようにそれを行うとdismissAndPop:中を宣言する必要があります.hと.m、

- (void)dismissAndPop:(UIAlertView *)alertView { 
    [alertView dismissWithClickedButtonIndex:0 animated:NO]; 
    [alertView release]; 

    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

3秒後に解雇を引き起こし、続いてpopViewControllerAnimated:と表示されます。

+0

こんにちはDeepak、 まず最初に、私の質問に非常に詳細に答えてくれてありがとう。私はちょうどあなたのような答えが私をたくさん助けてくれるようにiPhoneプログラミングを始めています。 私はあなたの措置に従い、要件を満たすことができました。 もう一度、Deepakのヘルプに感謝します。 :) – mmmrbm

0

する必要がありますメインスレッド以外の場所でUIで動作するコードを実行します。

UIで何かを変更するすべてのコードは、UIで行う必要があります。

また、[NSThread sleepForTimeInterval]を使用すると、あなたは処理が行われていることを知らせるデリゲートメソッドを持つことができない場合は悪い習慣であるブロッキング方法、(パッシブ待機対アクティブ待機)

であり、さらに代わりに一定の時間(秒)を待たせたい場合は、例えば、performSelector:withObject:afterDelay:を使用して、所定の遅延後にメソッドを呼び出すことができます。通常、このメソッドはalertViewを閉じてpopToRootViewControllerAnimatedメソッドを呼び出します。

その他のオプションはNSTimerを使用することですが、performSelector:withObject:afterDelay:は非常に使いやすいはずです。

+0

こんにちはアリ、 お返事ありがとうございます。私の間違いを指摘してくれてありがとう。上記のDeepakのコードを使用して要件を満たしています。あなたとディパクの両方に感謝します。 :) – mmmrbm

+0

ご意見ありがとうございます。しかし、単に感謝の言葉を使う代わりに答えを投票することを好む。例えば、私の答えがあなたにとって有益だったら、私の答えをupvoteすることができます(私の答えの左にある灰色の矢印をクリックしてください)。さらに、私たちのポストの1人があなたの質問に完全に答えたならば、答えの横にある√マークをクリックすることによって最良の答えを検証することを検討してください(そして、 – AliSoftware

関連する問題