ハマったこと
とある画面でモーダルで開く
let nextView = ViewController nextView.modalPresentationStyle = .fullScreen present(nextView, animated: true, completion: nil)
前の画面をpresentingViewControllerで取ろうとするがnilになる
guard let prevView = presentingViewController as? SearchFriendViewController else { print("Could not find previous viewController") return }
解決策
そういえば、モーダルを出してたのは起動した画面じゃなくタブバー画面かもしれない。
タブはすべてUINavigationControllerにしており、起動した画面はUINavigationControllerに含まれている。
UINavigationController自体がモーダルを起動することはないということか。たぶん。
UITabBarController
┗ UINavigationController
┃ ┗ UIViewController - UIViewController -> Modal呼び出し処理
┗ 実際のModalの場所
なのでUITabBarControllerからUINavigationControllerをたどって取得するとうまく行った。
これで良いんだろうか。。とりあえず階層構造を整理しておく必要がありそうだ。
// モーダルの起動元はタブコントローラ guard let tabcon = presentingViewController as? MainTabBarController else { print("Could not find tabbar Controller") return } // タブで選択中のナビゲーションコントローラ guard let navigation = tabcon.selectedViewController as? UINavigationController else { print("Could not find avigation nController") return } // ナビゲーションコントローラの最前面を取得 guard let prevView = navigation.topViewController as? SearchFriendViewController else { print("Could not find previous viewController") return }