0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VC間での値の受け渡し

Posted at

VC間での値の受け渡しについてざっくりとですが説明して行きたいと思います

####0.完成イメージ

https://twitter.com/kou09010901lam1/status/1336923280180092929?s=20####1. コード

####2.解説
左上のFILE->New->cocoa touch で ViewControler2 を作ります。
Viewcontrolerの値をViewControler2に渡します。
渡すのはテキストフィールに入力してその文字列を渡して行きたいと思います。

ViewControler

@IBOutlet weak var text: UITextField!
 var present = ""
override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view.
   }
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       let VC = segue.destination as! ViewController2
       VC.present2 = text.text!
   }

view controler2

    @IBOutlet weak var Label: UILabel!
    var  present2 = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        Label.text = present2
    }

#####override func prepare(for segue: UIStoryboardSegue, sender: Any?)
segueが実行される前に呼び出されるメソッドです。
overrideして

let VC = segue.destination as! ViewController2

遷移先画面のViewControllerを取得します

####3.関連記事
[Qlita]
[Qlita]
[Qlita]
[Qlita]
####4.最後に
簡単ですね。遷移先がナビゲーションコントローラーだった場合は少しややこしいので次回またあげてやっていきたいと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?