13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UIViewControllerの画面構成・画面遷移まとめ (iPhone向け)

Last updated at Posted at 2021-06-01

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.presentedViewControllerviewControllerB
  • viewControllerB.presentingViewControllerviewControllerA

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.topViewControllerviewControllerB
  • navigationController.visibleViewControllerviewControllerB
  • viewControllerA.parentnavigationController
  • viewControllerA.navigationControllernavigationController
  • viewControllerB.parentnavigationController
  • viewControllerB.navigationControllernavigationController

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.topViewControllerviewControllerB
  • navigationController.visibleViewControllerviewControllerC
  • navigationController.presentedViewControllerviewControllerC
  • viewControllerA.parentnavigationController
  • viewControllerA.navigationControllernavigationController
  • viewControllerA.presentedViewControllerviewControllerC
  • viewControllerB.parentnavigationController
  • viewControllerB.navigationControllernavigationController
  • viewControllerB.presentedViewControllerviewControllerC
  • viewControllerC.presentingViewControllernavigationController

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.selectedViewControllerviewControllerA
  • viewControllerA.parenttabBarController
  • viewControllerA.tabBarControllertabBarController
  • viewControllerB.parenttabBarController
  • viewControllerB.tabBarControllertabBarController

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.selectedViewControllerviewControllerB
  • tabBarController.presentedViewControllerviewControllerC
  • viewControllerA.parenttabBarController
  • viewControllerA.tabBarControllertabBarController
  • viewControllerA.presentedViewControllerviewControllerC
  • viewControllerB.parenttabBarController
  • viewControllerB.tabBarControllertabBarController
  • viewControllerB.presentedViewControllerviewControllerC
  • viewControllerC.presentingViewControllertabBarController

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.selectedViewControllernavigationController
  • viewControllerA.parenttabBarController
  • viewControllerA.tabBarControllertabBarController
  • navigationController.parenttabBarController
  • navigationController.tabBarControllertabBarController
  • navigationController.children[viewControllerB, navigationControllerC]
  • navigationController.viewControllers[viewControllerB, navigationControllerC]
  • navigationController.topViewControllerviewControllerC
  • navigationController.visibleViewControllerviewControllerC
  • viewControllerB.parentnavigationController
  • viewControllerB.navigationController navigationController
  • viewControllerB.tabBarController tabBarController
  • viewControllerC.parentnavigationController
  • viewControllerC.navigationController navigationController
  • viewControllerC.tabBarController tabBarController
13
10
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
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?