 
こんにちは、エンジニアのきくたにです。
Storyboardでページ遷移を行う場合、次のページに値を渡したいことがあると思います。
そこで今回はページ遷移をする場合の値渡しの方法を見ていきましょう。
(Swift5を使用しています)
目次
1. 完成イメージ
完成イメージはこのような感じです。
テキストフィールドに文字を入力して次へボタンを押すと、
次の画面にいきテキストフィールドに入力した文字がラベルに入ります。

2. ページ遷移する場合の値の渡し方
それではやっていきましょう
2-1. テキストフィールドと次へボタンを設置する
Main.storyboardにテキストフィールドと次へボタンを設置します。

2-2. コードと紐付ける
テキストフィールドと次へボタンをViewController.swiftと紐付けます。

2-3. 遷移先のページを作成する
Second.storyboardとSecondViewControllerを作成します。
Second.storyboardのクラスにSecondViewControllerを設定します

2-4. ラベルを設置する
Second.storyboardにラベルを設置します。

2-5. Storyboard IDを設定する
Second.storyboardのStoryboard IDに「SecondViewController」と設定します。

2-6. コードと紐付ける
テキストラベルをSecondViewController.swiftと紐付けます。

2-7. SecondViewControllerにコードを書く
SecondViewControllerにコードを書きます。
このときmakeInstance関数をつくるのがポイントです。
これをViewControllerから呼び出します。
makeInstance関数の中でtextLabelの値を受け取ります。
import UIKit
class SecondViewController: UIViewController {
    @IBOutlet weak var textLabelView: UILabel!
    private var textLabel: String?
    static func makeInstance(textLabel: String?) -> SecondViewController {
        let storyboard: UIStoryboard = UIStoryboard(name: "Second", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        viewController.textLabel = textLabel
        return viewController
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        textLabelView.text = textLabel
    }
}
2-8. ViewControllerにコードを書く
ViewControllerにコードを書きます。
このときSecondViewControllerのmakeInstance関数を呼び出します。
呼び出す際に引数にtextFieldの値をセットします。
ページ遷移の方法はこちらを見てみてください。
import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var textFieldView: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func nextButtonDidTap(_ sender: Any) {
        let viewController = SecondViewController.makeInstance(textLabel: textFieldView.text)
        self.present(viewController, animated: true, completion: nil)
    }
}
これで完成です。
3. おわりに
ページ遷移する場合の値の渡し方について説明していきました。
最後まで読んでいただきありがとうございました!

