5
6

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.

iPhone開発での画面遷移方法まとめ(Swift)

Posted at

最近iPhoneアプリの開発を始めてみて、最初に画面遷移するだけでとてもハマったのでメモしておく。
個人的にiPhoneの画面遷移って鬼門な気がしてる。Androidは簡単なのに... :thinking:

画面遷移いろいろ

同じ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

たぶんこれくらい知ってれば大丈夫なはず。。。

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?