56
63

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 5 years have passed since last update.

Swift初心者メモ Storyboardのsegueで遷移した画面に値を渡すす

Last updated at Posted at 2015-08-05

segueを使って画面遷移できたけど、値を引き継ぎたい時のメモ

FirstViewController

SecondViewController

にtextFieldの値を渡す想定

(ドットインストール参考)

※prepareForSegueというメソッドを使って渡すらしい
※segueのidentifierに名前つけとく 今回は(SecondView)

FirstViewController.swift
import UIKit

class ViewController: UIViewController {

    // テキストフィールド
    @IBOutlet weak var textField: UITextField!
    
    // このメソッドで渡す
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

      if segue.identifier == "SecondView" {

        let secondViewController:SecondViewController = segue.destinationViewController as SecondViewController

        // 変数:遷移先ViewController型 = segue.destinationViewController as 遷移先ViewController型
        // segue.destinationViewController は遷移先のViewController
        
        secondViewController.sendText= self.textField.text
      }
        
    }

}
SecondViewController.swift
import UIKit

class SecondViewController: UIViewController {
        
    // ここで受け取る
    var sendText:String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        println(sendText)
    }
}
56
63
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
56
63

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?