your3i’s blog

iOSエンジニア。頑張る⚉

presentingViewControllerとpresentedViewController

はじめに

presentingViewControllerとpresentedViewController、毎回どれがどれってわからなくなるので、今回徹底的に調査しようと。

とりあえず~それぞれの定義

presentingViewController

The view controller that presented this view controller.

Discussion

When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.

簡単に翻訳すると>>

モーダルで出したview controllerにとって、presentingViewControllerはこのview controllerを出したview controllerになる。もしview controllerはモーダルで出されたじゃなく、でもこのview controllerの親か(親の親か…先祖)はモーダルで出された場合、このview controllerのpresentingViewControllerはこの先祖のpresentingViewControllerになる。そのほかの場合、nilになる。

presentedViewController

The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.

Discussion

When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that called the method has this property set to the view controller that it presented. If the current view controller did not present another view controller modally, the value in this property is nil.

簡単に翻訳すると>>

present(_:animated:completion:) を呼び出す側のpresentedViewControllerはそのモーダルview controllerになる。

presentedViewController は結構わかりやすい気がする。

それでは謎解きタイム

1. presentingViewControllerとpresentedViewControllerの値はいつ設定される(いつ正しく取れる)?

viewDidLoad()のときにはまだ、viewWillAppear()呼ばれた時点で正しく取れた。


2. 各シナリオで、view controllerのpresentingViewControllerとpresentedViewControllerを検証

  • A →(present)→ B
vc presentingViewController presentedViewController
A nil B
B A nil
  • A →(present)→ B →(present)→ C
vc presentingViewController presentedViewController
A nil B
B A C
C B nil


下からは先祖があるパターン

  • UINavigationController →(root)→ A →(present)→ B
vc presentingViewController presentedViewController
UINavigationController nil B
A nil B
B UINavigationController nil
  • A →(present)→ UINavigationController →(root)→ B →(push)→ C
vc presentingViewController presentedViewController
A nil UINavigationController
UINavigationController A nil
B A nil
C A nil
  • UINavigationController1 →(root)→ A →(present)→ UINavigationController2 →(root)→ B →(push)→ C
vc presentingViewController presentedViewController
UINavigationController1 nil UINavigationController2
A nil UINavigationController2
UINavigationController2 UINavigationController1 nil
B UINavigationController1 nil
C UINavigationController1 nil


先祖があるパターンからみると、UINavigationControllerの中のview controllerたちはモーダルview controller のこと知ってるけど、モーダルview controllerはUINavigationControllerのことしか知らない。
presentingViewControllerとpresentedViewControllerの関係は多分present(_:animated:completion:) methodで繋がった二つのview controller groupの関係で、一つのview controller groupの中のview controllerはもう一つのview controller groupの一番のparent view controller しか知らない。