質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Q&A

1回答

3335閲覧

タイマー終了後、アラートが既に表示されている時、UIAlertControllerで同時にアラートが表示できない

skbhry

総合スコア13

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

0グッド

0クリップ

投稿2016/09/23 04:29

iOSのアプリでUIAlertControllerを使用して、一定時間が経つとログアウトポップアップが出て、OKを押すとログアウトされるタイマーを作成しています。

ポップアップやアラート等が出ていない通常のWebViewの画面ではタイマーが起動してログアウトのポップアップを表示してくれるのですが、
他のポップアップ等が出ている時にはログアウトのアラートが出てくれません。
ログを出してみたところ、

Warning: Attempt to present <UIAlertController: 0x7fc764612600> on <WebViewController: 0x7fc761f970a0> which is already presenting (null)

と、ログアウトしてほしい秒数までカウントされた後、表示されてしまっています。

UIAlertControllerではアラートを複数同時に出せないことは調べてわかりましたが、そのアラートを無視?して、ログアウトのアラートを出し、OKを押してログアウトさせる方法がわかりません。
恐縮ですが、ご教授願います。

Objective

1 2/* WebViewController */ 3 4/* 最終操作の有効チェック */ 5- (void)checkTimeFromLatestOperation:(NSTimer *)timer 6{ 7 // 現在時刻と最終操作時間の差分が24分以上の場合、ログアウトする 8 NSTimeInterval now = [NSDate date].timeIntervalSince1970; 9 10 AppSettingsUtility *utility = [[AppSettingsUtility alloc] init]; 11 NSTimeInterval latestTime = [utility latestOperationTime]; 12 13 NSTimeInterval diff = now - latestTime; 14 NSLog(@"%f", diff); 15 if (diff >= [utility timeOutSeconds]) { 16 [self showLogoutAlert:NSLocalizedString(@"ForceLogoutMessage", 17 @"強制ログアウト時のメッセージ") 18 isForced:YES]; 19 // 有効チェックのタイマーをストップする 20 [_timer invalidate]; 21 _timer = nil; 22 } 23} 24 25 26/* ログアウトのアラート表示 */ 27- (void)showLogoutAlert:(nonnull NSString *)message isForced:(BOOL)isForced 28{ 29 UIAlertController *alert = 30 [UIAlertController alertControllerWithTitle:nil 31 message:message 32 preferredStyle:UIAlertControllerStyleAlert]; 33 UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) 34 style:UIAlertActionStyleDefault 35 handler:^(UIAlertAction *_Nonnull action) { 36 [self requestLogoutApiConnector]; 37 }]; 38 [alert addAction:okAction]; 39 if (!isForced) { 40 UIAlertAction *cancelAction = 41 [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) 42 style:UIAlertActionStyleCancel 43 handler:^(UIAlertAction *_Nonnull action){ 44 }]; 45 [alert addAction:cancelAction]; 46 } 47 [self presentViewController:alert animated:YES completion:nil]; 48} 49

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

コードの最後の部分

objective

1 [self presentViewController:alert animated:YES completion:nil];

この部分を

objective

1 UIViewController *baseView = [UIApplication sharedApplication].keyWindow.rootViewController; 2 while (baseView.presentedViewController != nil && 3 !baseView.presentedViewController.isBeingDismissed) { 4 baseView = baseView.presentedViewController; 5 } 6 [baseView presentViewController:alert animated:YES completion:nil];

に変更しました。しかし、理由がまだちゃんとわかっていないので、わかる方、よければ解説をお願い致します。

投稿2016/09/23 06:54

skbhry

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問