2011-04-28 19 views
3

UIAlertViewをテストするためのiOSアプリケーションを作成しました。私がそれを走らせると、UIAlertViewが表示され、画面が暗くなり、UIAlertViewがなくなり、「長い時間が経過しました」とUIAlertViewが表示されました。これは、SimulatorとiPhone(IOS 4.2)の両方で発生します。なぜか分からない、助けてくれて、ありがとう。UIAlertViewが非常にゆっくりと表示されます

説明:(あなたはまた、プロジェクトHEREをダウンロードすることができます)

その3クラスと非常にシンプルなビューベースのアプリケーション:AppDelgate ViewControlerとNSOperationを実装TestOperation。 AppDelegateは、XCodeによって作成されたものです。 TestOperation.h:解決

#import <Foundation/Foundation.h> 

@protocol TestOperationDelegate 

- (void)didFinishTestOperaion; 

@end 


@interface TestOperation : NSOperation { 
    id <TestOperationDelegate> delegate; 
} 

@property (nonatomic, assign) id <TestOperationDelegate> delegate; 

@end 

TestOperation.m

#import "TestOperation.h" 


@implementation TestOperation 

@synthesize delegate; 

- (void)main { 
    [delegate didFinishTestOperaion]; 
} 

@end 

ViewContoller.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    TestOperation *testOperation = [[TestOperation alloc] init]; 
    testOperation.delegate = self; 
    [queue addOperation:testOperation]; 
    [testOperation release]; 
} 

- (void)didFinishTestOperaion { 
    NSLog(@"start uialertview"); 
    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Oops!" message:@"Here's the UIAlertView" 
          delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

// !!メインスレッド上 UIの実行を作るためにperformSelectorOnMainThreadを使用して、あなたがUIAlertViewでテストしようとしている何

+1

解決策を以下の回答として投稿し、回答があればそれを受け入れてください。 – titaniumdecoy

答えて

8

解決済み!メインスレッドでUI実行

[:@selector(ショー)withObject:waitUntilDoneゼロ:警告performSelectorOnMainThread NO]を作るためにperformSelectorOnMainThreadを使用します。

+1

performSelectorOnMainThreadの使い方を投稿してください。 – rmp2150

1

?あなたが単にviewDidAppearからUIAlertViewを呼び出した場合:あなたのViewControllerで、UIAlertViewが期待どおりに素早く表示されていますか?

あなたの問題は、UIAlertViewをどのように呼び出すかに関係しており、ViewControllerによって制御されるUIViewが表示される前にUIAlertViewが表示されていると思います。

+0

ビューにボタンを追加し、NSOperationQueueパートをイベントリスナー内のタッチアップボタンに移動して、viewControllerが表示されていることを確認します。ボタンをタッチすると、UIAlertViewは以前と同じように動作します。私がviewDidLoadからUIAlertViewを呼び出したばかりであれば、それは正しく動作します。なぜなら、私はデリゲートを使用しているだけかもしれないと思うかもしれませんが、これを解決する方法はわかりません。 – ultragtx

関連する問題