Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

下記の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

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした