LoginSignup
25
8

More than 1 year has passed since last update.

[iOS] [Swift] dismissの仕様を勘違いしていた件

Last updated at Posted at 2021-02-27

前提環境

  • iOS 14.4
  • Xcode 12.1 / Swift 5.3

疑問

私は、UIViewControllerのdismiss(:animated)はシンプルに「自身を閉じる」メソッドだと思い込んでいたのですが、どうやら勘違いでした。
同じ勘違いをされている方も案外多いのでは?と考えてこの記事を書きました。

子でdismissを実行すると、子自身が閉じます。
これは直感通りの挙動です。
図1.png

一方、Notificationやdelegateなどを介して親で親自身のViewControllerに対してdismissを実行すると、子が閉じて親は閉じません。
この挙動が私には意味不明でした。
図1.png

わかったこと

公式リファレンスより:

calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion

訳:スタックの下位のView Controllerでこのメソッドを呼び出すと、**その直接の子ViewControllerとスタック上のその子の上のすべてのViewControllerが閉じられます。**これが発生すると、最上部のビューのみがアニメーション化されて閉じられます。

子でdismissを実行すると子自身が閉じることも、
親でdismissを実行すると子が閉じて親が閉じないことも、
dismissの仕様なんですね。

25
8
2

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
25
8