UIKit による iOSアプリの画面は1つ以上の UIViewController で構成され、画面遷移も UIViewController により行われます。また、UIViewController間の関連性は UIViewController のプロパティにより把握することができます。
この記事では iPhone 向けの画面構成・画面遷移、そしてその関連性について整理してまとめたものです。主に iPad 向けの Split View や Popover については触れません。
1. UIViewController
1つの UIViewController だけで構成される最もシンプルな状態。
let viewControllerA = UIViewController()
window.rootViewController = viewControllerA
2. UIViewController → UIViewController (Present Modally)
UIViewController の present(_:, animated:)
メソッドにより、別の UIViewController をモーダル表示。
let viewControllerA = UIViewController()
window.rootViewController = viewControllerA
let viewControllerB = UIViewController()
viewControllerA.present(viewControllerB, animated: true)
-
viewControllerA.presentedViewController
→viewControllerB
-
viewControllerB.presentingViewController
→viewControllerA
3. UIViewController → UIViewController (Embed)
UIViewController に別の UIViewController を組み込み、コンテナとして振る舞う。
let viewControllerA = UIViewController()
window.rootViewController = viewControllerA
let viewControllerB = UIViewController()
viewControllerA.addChild(viewControllerB)
viewControllerA.view.addSubview(viewControllerB.view)
viewControllerB.didMove(toParent: viewControllerA)
-
viewControllerA.children
→[viewControllerB]
-
viewControllerB.parent
→viewControllerA
4. UINavigationController → UIViewController (Root) → UIViewController (Show Detail)
UINavigationController という特殊な UIViewController。複数の UIViewController を扱うコンテナとして振る舞い、ドリルダウン形式の画面遷移を行う。
let viewControllerA = UIViewController()
let navigationController = UINavigationController(rootViewController: viewControllerA)
window.rootViewController = navigationController
let viewControllerB = UIViewController()
viewControllerA.navigationController?.pushViewController(viewControllerB, animated: true)
-
navigationController.children
→[viewControllerA, viewControllerB]
-
navigationController.viewControllers
→[viewControllerA, viewControllerB]
-
navigationController.topViewController
→viewControllerB
-
navigationController.visibleViewController
→viewControllerB
-
viewControllerA.parent
→navigationController
-
viewControllerA.navigationController
→navigationController
-
viewControllerB.parent
→navigationController
-
viewControllerB.navigationController
→navigationController
5. UINavigationController → UIViewController (Root) → UIViewController (Show Detail) → UIViewController (Present Modally)
UINavigationController から別の UIViewController をモーダル表示。
let viewControllerA = UIViewController()
let navigationController = UINavigationController(rootViewController: viewControllerA)
window.rootViewController = navigationController
let viewControllerB = UIViewController()
viewControllerA.navigationController?.pushViewController(viewControllerB, animated: true)
let viewControllerC = UIViewController()
viewControllerB.present(viewControllerC, animated: true)
-
navigationController.children
→[viewControllerA, viewControllerB]
-
navigationController.viewControllers
→[viewControllerA, viewControllerB]
-
navigationController.topViewController
→viewControllerB
-
navigationController.visibleViewController
→viewControllerC
-
navigationController.presentedViewController
→viewControllerC
-
viewControllerA.parent
→navigationController
-
viewControllerA.navigationController
→navigationController
-
viewControllerA.presentedViewController
→viewControllerC
-
viewControllerB.parent
→navigationController
-
viewControllerB.navigationController
→navigationController
-
viewControllerB.presentedViewController
→viewControllerC
-
viewControllerC.presentingViewController
→navigationController
6. UITabBarController → [UIViewController, UIViewController]
UITabBarController という特殊な UIViewController。複数の UIViewController を扱うコンテナとして振る舞い、タブ形式の画面遷移を行う。
let tabBarController = UITabBarController()
window.rootViewController = tabBarController
let viewControllerA = UIViewController()
let viewControllerB = UIViewController()
tabBarController.viewControllers = [viewControllerA, viewControllerB]
-
tabBarController.children
→[viewControllerA, viewControllerB]
-
tabBarController.viewControllers
→[viewControllerA, viewControllerB]
-
tabBarController.selectedViewController
→viewControllerA
-
viewControllerA.parent
→tabBarController
-
viewControllerA.tabBarController
→tabBarController
-
viewControllerB.parent
→tabBarController
-
viewControllerB.tabBarController
→tabBarController
7. UITabBarController → [UIViewController, UIViewController] → UIViewController (Present Modally)
UITabBarController から別の UIViewController をモーダル表示。
let tabBarController = UITabBarController()
window.rootViewController = tabBarController
let viewControllerA = UIViewController()
let viewControllerB = UIViewController()
tabBarController.viewControllers = [viewControllerA, viewControllerB]
tabBarController.selectedIndex = 1
let viewControllerC = UIViewController()
viewControllerB.present(viewControllerC, animated: true)
-
tabBarController.children
→[viewControllerA, viewControllerB]
-
tabBarController.viewControllers
→[viewControllerA, viewControllerB]
-
tabBarController.selectedViewController
→viewControllerB
-
tabBarController.presentedViewController
→viewControllerC
-
viewControllerA.parent
→tabBarController
-
viewControllerA.tabBarController
→tabBarController
-
viewControllerA.presentedViewController
→viewControllerC
-
viewControllerB.parent
→tabBarController
-
viewControllerB.tabBarController
→tabBarController
-
viewControllerB.presentedViewController
→viewControllerC
-
viewControllerC.presentingViewController
→tabBarController
8. UITabBarController → [UIViewController, UINavigationController → UIViewController (Root) → UIViewController (Show Detail)]
UITabBarController と UINavigationController の組み合わせ。
let tabBarController = UITabBarController()
window.rootViewController = tabBarController
let viewControllerA = UIViewController()
let viewControllerB = UIViewController()
let navigationController = UINavigationController(rootViewController: viewControllerB)
tabBarController.viewControllers = [viewControllerA, navigationController]
tabBarController.selectedIndex = 1
let viewControllerC = UIViewController()
navigationController.pushViewController(viewControllerC, animated: true)
-
tabBarController.children
→[viewControllerA, navigationController]
-
tabBarController.viewControllers
→[viewControllerA, navigationController]
-
tabBarController.selectedViewController
→navigationController
-
viewControllerA.parent
→tabBarController
-
viewControllerA.tabBarController
→tabBarController
-
navigationController.parent
→tabBarController
-
navigationController.tabBarController
→tabBarController
-
navigationController.children
→[viewControllerB, navigationControllerC]
-
navigationController.viewControllers
→[viewControllerB, navigationControllerC]
-
navigationController.topViewController
→viewControllerC
-
navigationController.visibleViewController
→viewControllerC
-
viewControllerB.parent
→navigationController
-
viewControllerB.navigationController
→navigationController
-
viewControllerB.tabBarController
→tabBarController
-
viewControllerC.parent
→navigationController
-
viewControllerC.navigationController
→navigationController
-
viewControllerC.tabBarController
→tabBarController