2
3

More than 3 years have passed since last update.

Storyboardが複数の場合の画面遷移

Last updated at Posted at 2020-04-07

はじめに

Storyboard:ViewController=1:1で開発する時に画面遷移ってどうするんだろう、と思ったのでやってみました。
そこら中に記事があるので、自分用の備忘録として残しておきます。
想定は「Main.storybard」→「Sub.storyboard」の画面遷移です。

手順

1.Main.storyboardを開いて画面遷移する用のボタンを用意する。
main.png

MainViewController.swift
import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func tapBtn(_ sender: Any) {
        // Storyboardのインスタンスを名前指定で取得する
        let storyboard = UIStoryboard(name: "Sub", bundle: nil)
        // Storyboard内で'is initial'に指定されているViewControllerを取得する
        let nextVC = storyboard.instantiateInitialViewController() as! SubViewController
        // FullScreenにする
        nextVC.modalPresentationStyle = .fullScreen
        // Subへ渡す値
        nextVC.text = "hello" // <--- SubViewControllerのプロパティ`text`
        // presentする
        self.present(nextVC, animated: true, completion: nil)
    }
}

2.Sub.storyboardSubViewController.swiftを作成する
sub.png

この時、Is Initial ViewControllerチェックを入れることを忘れないように!
スクリーンショット 2020-04-07 10.17.23.png

3.起動すれば動くはず

参考

2
3
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
2
3