LoginSignup
4
1

More than 3 years have passed since last update.

画面遷移時の値渡しのやり方【Swift 5】

Last updated at Posted at 2021-02-19

はじめに

この記事では、異なるファイル間で(classを跨いで)値をやりとりする方法を紹介していきます。
また、付随するものとして、segueを使わずに画面遷移を行う方法も紹介していきます。

環境

・macOS catalina
・Xcode version 12.2
・swift 5.3.1

コードを用いた画面遷移

まずは遷移先の情報を取得します。
この際、""内にはStoryBoard IDを、as!以下は、遷移先のcocoa touch ファイルの名前を入力します。
スクリーンショット 2021-02-20 0.27.26.png
なお、StoryBoard IDは写真の赤丸で囲われた部分から入力します。この時、Use StoryBoard IDに忘れずにチェックを入れるようにしてください。

//遷移先の情報の取得
let nextView = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController



次に、遷移の方法を指定します。(任意)
上の行では、遷移先の画面をどう表示するかを指定することができます。ここでは、.coverVertical, crossDissolveなどの方法を指定することができます。
下の行では、どのようなアニメーションで遷移を行うのかを指定することができます。

nextView.modalTransitionStyle = .crossDissolve   //遷移の仕方の設定
nextView.modalPresentationStyle = .fullScreen    //遷移先の表示方法の設定



最後に、このコードを実行すると遷移が完了します。

self.present(nextView, animated: true, completion: nil)    //遷移の実行


コードでの遷移を行うメリットとしては、segueを使うよりも、画面遷移の際のイベントを実装しやすいことが挙げられます。
したがって、遷移の際に何らかの処理を行う際には、上記のようにコードを用いた方法で行う方が良いです。

最後に、もう一度全体のコードを載せておきます。

let nextView = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
nextView.modalTransitionStyle = .crossDissolve
nextView.modalPresentationStyle = .fullScreen
self.present(nextView, animated: true, completion: nil)

遷移先に値を渡す

ここでは、SecondViewController.swiftに、FirstViewController.swiftから値を渡す場合を考えます。

まずは、値の受け手である、SecondViewController.swiftに、以下のように記述します。

//SecondViewController.swift
import UIKit

class SecondViewController: UIViewController{
      override func viewDidLoad() {
          super.viewDidLoad()
      }
      //FirstViewControllerから受け取る値をいれる変数の宣言
      var reciever = ""
}

この際、reciverが、値を受け取る変数です。実際に使うときは、ここに、値を受け取る変数を自由にセットしてください。

続いては、FirstViewController.swiftの中に、SecondViewController.swiftに値を渡す処理を入力していきます。

//FirstViewController.swift
import UIKit

class FirstViewController: UIViewController{
      override func viewDidLoad() {
          super.viewDidLoad()
      }

      //→ここから遷移のコードなので、実際は遷移したいタイミングで実行される場所に書きます (ex. IBActionの処理の中など)
      //遷移先を取得(上記参照)
      let nextView = storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! ViewController
      //""内には、SecondViewControllerのstoryBoard IDを入力します
      nextView.modalTransitionStyle = .crossDissolve
      nextView.modalPresentationStyle = .fullScreen

      //SecondViewController.swift内の変数recieverに文字列"Hello, world!"を代入
      nextView.reciever = "Hello, world!"

      //遷移を実行
      self.present(nextView, animated: true, completion: nil)
}

上記の手順で、異なるファイル間での値渡しは完了です。

注意事項

遷移先(SecondViewController)にIBActionやIBOutlet接続されている値(LabelやTextfieldなど)は遷移時に指定してしまうとエラーが出てしまいます。
そのような時は一旦遷移先(SecondViewController)に一時的に値を受け取る変数を適当に宣言して、そちらに値を渡して、遷移後にSecondViewControllerのViewDidLoadなどでIBActionやIBOutlet接続されている値に代入するようにしてください。

自作アプリの宣伝

私たちは、自作のリマインダーアプリ「タスクリマインダー -TaskReminder 課題管理-」、決断代行アプリ「決断メーカー」をAppStoreにて公開しています。

~ダウンロードはこちらから~
タスクリマインダー -TaskReminder 課題管理-
決断メーカー


4
1
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
4
1