[swift]モーダルで表示したViewControllerの後ろにあるViewControllerを操作する
モーダル表示したViewControllerの後ろのViewControllerを操作したいって時。
インスタなんかはTabbarの真ん中の投稿ボタンを押すと、投稿画面がモーダルで下からスイっと出てきて、
投稿完了するとスイっと投稿画面が下に下がって、投稿一覧に戻りますよね。
それをしたい
動きを整理すると
-
- •
- 投稿が完了する
-
- •
- 後ろのViewControllerを操作する
-
- •
- モーダルを閉じる
ようはdismissで閉じる前に後ろのモーダルを操作すればよい。
今回、タブバーで紐づいてるそれぞれのViewControllerは、NavigationControllerをembed in させてます。
インスタみたいにモーダル表示投稿画面を投稿完了してモーダルが閉じると、すでにTopページが表示されてる。みたいなイメージ
//投稿保存を実行
share.save()
//ここから
// まずは親の(後ろの)タブバーのインスタンスを取得
if let tabvc = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController {
//左から0番目のタブアイコンを選択状態にする(0が一番左)
DispatchQueue.main.async {
tabvc.selectedIndex = 0
}
}
//移動先ViewControllerのインスタンスを取得(storyboard id: MainTabBarController)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let toTop = storyboard.instantiateViewController(withIdentifier: "MainTabBarController")
self.tabBarController?.navigationController?.present(toTop, animated: true, completion: nil)
//遷移実行したら投稿画面を閉じる
self.dismiss(animated: true, completion: nil)



