LoginSignup
4
3

More than 3 years have passed since last update.

エラー「Application tried to present modally a view controller」の時

Posted at

環境

・Xcode: 12.3
・Swift5

以下のようなエラーログがでてクラッシュしてしまった時

libc++abi.dylib: terminating with uncaught exception of type NSException

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
 reason: 'Application tried to present modally a view controller <ProjectName.ViewController: 0x158331780> 
.that has a parent view controller <UINavigationController: 0x159877a00>.'
terminating with uncaught exception of type NSException

こんなパターンがあるよ

スクリーンショット 2021-01-25 23.26.29.png

上記のようにViewControllerに対してNavigationControllerがいる場合に、
ViewControllerを表示しようとしてしまっている可能性


        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        if let navigationVc = storyboard.instantiateInitialViewController() as? UINavigationController,
           let viewController = navigationVc.topViewController as? ViewController {
            // これは×
            // present(viewController, animated: true, completion: nil)
            // これは○
            present(navigationVc, animated: true, completion: nil)
        }

これに限らないかと思いますが、誰かの役に立てば。

4
3
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
4
3