利用 segue 連結

比柯
Jun 28, 2021

前言:

performSegue 是拿 segueID來轉場頁面

prepare for segue是向指定的Controller傳送資料的功能

(let vcOne = segue.destination as?VCtwo)

view或image可利用程式來轉場

VC one 加入一個Button 為 (next)

VC two 加入一個Button 為 (exit)

按住 control 從(next) Button 拖曳到VC two上 選擇 “SHOW”

從 exit Button返回 VC one

創建VC two的ViewController

拉入exit 的 IBAction

以下程式碼

@IBAction func exitButton(_ sender: UIButton) {

dismiss(animated: true, completion: nil)

}

VC one 將值傳到 VC two

拖曳一個 Click Button

VC two設一個變數來接值

var receive: String?

VC one 鍵入”prepare”

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

let vcOne = segue.destination as?VCtwo (向下轉型的地方)

vcOne?.receive = “Hello world” (接值的變數要做什麼?)

}

在VC two 建立Click Button的@IBAction

@IBAction func clickButton(_ sender: UIButton) {

if let receive = receive {

(新變數 = 傳值的變數)

print(receive)

}

editor -> Embed in -> Navigation Controller

兩個以上的segue

在segue的idrntifier id取名 segue_to_VCtwo

在prepare function內加入

if segue.identifier == “segue_to_VCtwo” {

}

--

--