LoginSignup
0
3

【Swift】画面の戻り方のまとめ

Last updated at Posted at 2021-11-25

Swiftで画面を戻る処理が何種類かあるのでまとめてみました。

モーダル型で戻る

self.dismiss(animated: true, completion: nil)

dismissで画面を破棄しています。

モーダル型で2つ前の画面に戻る

self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

2つ前の画面に戻るコードです。

1つ前の画面へ戻る(NavigationController)

self.navigationController?.popViewController(animated: true)

NavigationControllerで1つ前の画面へ戻る処理。

トップの階層へ戻る(NavigationController)

self.navigationController?.popToRootViewController(animated: true)

NavigationControllerでトップの階層へ戻る処理。

任意の階層へ戻る(NavigationController)

self.navigationController?.popToViewController(navigationController!.viewControllers[階層の番号], animated: true)

NavigationControllerで任意の階層へ戻る処理。

*戻るだけで先に進むことはできません。

参考

https://naoya-ono.com/swift/navigation-controller-back/
https://qiita.com/Walkdream24/items/787df7d5b6e85330416a

Swiftのお役立ち情報

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