hachinoBlog

hachinobuのエンジニアライフ

UIAlertがフリーズする現象

UIAlertViewを表示した瞬間にホームボタンでバックグラウンドへいき再度アプリを開くとUIAlertViewが出たまま画面がフリーズ状態になった。
問題が起きたのは下記コード

+ (void) simpleSuccessWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate {
	
	UIAlertView *alert = [[[UIAlertView alloc] init] autorelease];
    
	[alert setTitle:title];
	[alert setCancelButtonIndex:0];
	[alert setMessage:message];
	[alert setDelegate:delegate];
	[alert show];
	
	[self performSelector:@selector(dismissSuccess:) withObject:alert afterDelay:1.5f];
}  

+ (void) dismissSuccess:(UIAlertView *)alert {
    if (alert.visible) {
	[alert dismissWithClickedButtonIndex:alert.cancelButtonIndex animated:YES];
    }
}

原因としてはアラートを閉じる処理で現在アラートが表示されているかをalert.visibleで判定しているが、
アラートが出ている状態でホームボタンでバックグラウンドにいき、またアプリを開いた状態にするとalert.visibleが
アラートはまだ出ているにも関わらずNOを返すことが判明。(本来ならばアラートが表示されているのでYESを期待していた)
なのでdismissSuccessメソッドを下記のように判定をなくすよう修正するとフリーズしなくなりました。

+ (void) dismissSuccess:(UIAlertView *)alert {
  [alert dismissWithClickedButtonIndex:alert.cancelButtonIndex animated:YES];
}