0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NavigationControllerで戻る画面を自在に書き換える

Last updated at Posted at 2020-07-08

NavigationControllerを利用したページ移動をしていて、戻るページのスタックを自分の好きなように書き換えたくなった時があった。2ページ前に戻るとか、ある条件を満たした時だけ戻れるページの上限が変わるなど。

NavigationController.viewControllerの仕組み

navigationController?.viewControllers // [1pページ目, 2pページ目, 3pページ目, 今のページ]

NavigationControllerは上記のような感じで配列としてこれまで遷移したページを持っている。
基本的にはこれをpopしたりpushしたりする事で1画面遷移する。

ViewControllerが入った配列

ViewControllerが入った配列なので、この配列を好きなように操作してやれば戻るページを書き換えられる。

ただ、基本的にはスタックとして使う事を想定しているはずなので行儀は良くない気がする。

UIViewControllerクラス内で

let fugaView = self.storyboard?.instantiateViewController(withIdentifier: "FugaViewWrapController") as! FugaViewController
let hogeView = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController            
            
self.navigationController?.viewControllers = [fugaView, hogeView, self]

こうしてやると、これまでどんな遷移をしていようが次 hogeView → fugaView の順番に戻るようになる。

こんな感じで好きなようにカスタマイズ出来る、と思う。多分。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?