LoginSignup
0
1

More than 3 years have passed since last update.

UIAlertViewControllerをpresentで表示中はUINavigationControllerのスタックを更新できない

Posted at

複数のUIAlertViewControllerがキューイングされたうえで同時にpresentされるようなイベントがあり、その時に表題の仕様を知らずハマりました。

ハマった一例

    // ①イベントの例外処理等で呼ばれるアラート
    func showCancelOperationDialog() {

        let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
        // OKアクション
        let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: { _ in

            self.moveToFirstViewController()
        })
        // キャンセルアクション
        let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

    // ②特定のエラーで呼ばれるアラート
    func showExceptionErrorDialog() {

        let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
        // OKアクション
        let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        // キャンセルアクション
        let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

    // 特定のViewControllerへ戻る処理
    func moveToFirstViewController() {

        guard let firstViewController = self.navigationController?.viewControllers.first as? FirstViewController else {

            return
        }
        self.navigationController?.popToViewController(firstViewController, animated: true)
    }

上記のアラート2種が同時に呼び出され、①イベントの例外処理等で呼ばれるアラート -> ②特定のエラーで呼ばれるアラートの順で実行された場合、①のOKアクションでpopToViewControllerが行われません。正確には呼び出しされているのですが表題の通り、presentで表示中はUINavigationControllerのスタックを更新できないため、画面遷移が行われません。

回避策

任意のViewControllerに遷移してからアラートを表示させるようにしました。
例外処理としての画面遷移なので遷移 -> アラート表示の順が好ましいですね。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1