【Xcode/Swift】画面遷移チートシート

コードのみで遷移

modal遷移

let vc = self.storyboard?.instantiateViewController(withIdentifier: "toNext") as! NextViewController
self.present(vc, animated: true, completion: nil)

push遷移

let vc = self.storyboard?.instantiateViewController(withIdentifier: "toNext") as! NextViewController
self.navigationController?.pushViewController(vc, animated: true)

コードのみで異なるStoryboardに遷移

let storyboard = UIStoryboard(name: "Storyboardのファイル名", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ボードのID") as! ボードに紐づくViewController
self.navigationController?.pushViewController(vc, animated: true)

コードで前の画面に戻る

modal遷移

dismiss(animated: true)

push遷移

一番最初に戻る

self.navigationController?.popToRootViewController(animated: true)