0
1

More than 3 years have passed since last update.

【超初学者向け】画面遷移と値の引き渡しをコードで同時に行う。

Posted at

はじめに

本記事は、筆者が備忘録として作成したものである。
また同時に超初学者向けの簡単な内容となっております。

実装したいこと

今回は、Swift学習の超基本である、下記2点について記述する。
①任意のタイミングでの画面遷移
②値の引き渡し(画面遷移と同時に処理)

今回は、ViewContorollerのラベルデータを→NextViewController(遷移先)のラベルへ反映させたいと思います。

必要なコード

必要なコードは下記のみ

①「任意のタイミングでの画面遷移」

ViewController
//ボタンがタップされた時に画面を遷移する場合。
@IBAction func tapButton(_ sender: Any) { 

performSegue(withIdentifier: "遷移先と繋がるSegueのidentifier", sender: nil)

}

②「任意のタイミングでの画面遷移」

ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

let nextVC = segue.destination as? NextViewController //←遷移先名:NextViewController
nextVC?.NextViewLabel = Labal.text! //ラベル(Label.text)に入っているデータを遷移先のラベル(NextViewLabel:String型)へ引き渡す。

}

③遷移先での処理

NextViewController

let NextViewLabel = String() //String型で受け取る。
NextLabel.text = NextViewLabel // textに表示。

以上で完成です。

結果

画面遷移と値の受け渡しが任意のタイミングで出来る。

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