95
83

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.

同じ/異なるStoryboardでの画面遷移

Last updated at Posted at 2015-03-09

下記の2つのStoryboard (Main, Another) と、それに紐付く3つのViewControllerがあったとして、ViewController1 から ViewController2 への同じStoryboard内での画面遷移と、ViewController2 から ViewController3 への異なるStoryboardでの画面遷移の方法をまとめます。

  • Main.storyboard
  • ViewController1 (Initial View Controller)
  • ViewController2
  • Another.storyboard
  • ViewController3 (Initial View Controller)

同じStoryboard内での遷移

Main内の ViewController1 から ViewController2 に遷移。

Segueを利用する

ユーザ側のアクションで遷移させたい場合 (例: ボタンをタップしたら遷移)

Storyboard上で、ViewController1 の UIButton などから ViewController2 にSegueを繋ぐ (のみでOK)。

sc.png

アプリ側で画面遷移を制御したい場合 (例: データを受信したら遷移)

  1. Storyboard上で、ViewController1 の View Controller から ViewController2 にSegueを繋ぐ。
  2. Segueの名前を設定。

sc2b.png

  1. 遷移させたい場面で下記のコードを記載。
ViewController1.swift
performSegueWithIdentifier("screenTransition", sender: self)

Segueを使用しない

遷移させたい場面で下記のコードを記載。

ViewController1.swift
let next: UIViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! UIViewController
presentViewController(next, animated: true, completion: nil)

異なるStoryboardへの遷移

Main内の ViewController2 から Another内の ViewController3 に遷移。

ViewController2.swift
let storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
let next: UIViewController = storyboard.instantiateInitialViewController() as! UIViewController
presentViewController(next, animated: true, completion: nil)

ViewController3 が Another.storyboard の Initial View Controller に設定されていれば、コントローラ名の明記は不要。

sc3.png

95
83
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
95
83

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?