最近iPhoneアプリの開発を始めてみて、最初に画面遷移するだけでとてもハマったのでメモしておく。
個人的にiPhoneの画面遷移って鬼門な気がしてる。Androidは簡単なのに...
画面遷移いろいろ
同じStoryboard のViewControllerへ
let next = self.storyboard!.instantiateViewController(withIdentifier: "Next1ViewController")
self.present(next, animated: true)
別のStoryboardのViewControllerへ
let storyboard = UIStoryboard(name: "Next", bundle: nil)
let next = storyboard.instantiateViewController(withIdentifier: "Next2ViewController")
self.present(next, animated: true)
NavigationControllerのあるStoryboardへ
let storyboard = UIStoryboard(name: "Navi", bundle: nil)
let next = storyboard.instantiateViewController(withIdentifier: "Next3ViewController")
self.present(next, animated: true)
NavigationControllerを持つViewControllerでNavigationControllerを使って画面遷移
let next = self.storyboard?.instantiateViewController(withIdentifier: "Next4ViewController")
self.navigationController?.pushViewController(next!, animated: true)
TabbarControllerのあるStoryboardへ
let storyboard = UIStoryboard(name: "Tab", bundle: nil)
let next = storyboard.instantiateViewController(withIdentifier: "Next5ViewController")
self.present(next, animated: true)
TabbarControllerを持つViewControllerでTabbarControllerを使って画面遷移
※Storyboard上でAttribute InspectorでBarItemのtagに番号つけておく
self.tabBarController?.selectedIndex = 1
rootViewControllerを直接入れ替える
let storyboard = UIStoryboard(name: "Tab", bundle: nil)
let next = storyboard.instantiateViewController(withIdentifier: "Next5ViewController")
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window?.rootViewController = next
たぶんこれくらい知ってれば大丈夫なはず。。。