セグエ

はじめに

segueの理解したとこアウトプット

実行環境

  • swift : 4.2.1
  • xcode : 10.1

コード

class TimeView : UIViewController {

    var set_hour: Int = 1
    var set_min: Int = 2
    var set_sec: Int = 3

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

    override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    // 値渡し
    @IBAction func startSegue(_ sender: Any) {
        performSegue(withIdentifier: "NextView", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let next = segue.destination as? NextView
        let _ = next?.view
        next?.count_hour = self.set_hour
        next?.count_min = self.set_min
        next?.count_sec = self.set_sec
    }
}

アウトプット

  • storyboardで遷移先にcontrol接続()
  • storyboardで繋いだ矢印をクリックしてwithIdentifierの名前を遷移先のクラス名にする(ここではNextView)
  • 遷移前のクラスで上のコードを記述
  • func prepareの最初2行の記述で、遷移先の変数にアクセスできる(next?.遷移先の変数)
  • そいつに遷移前の値を代入すればok。向こうで取り出せる

課題

  • オートレイアウト