簡體   English   中英

如何通過閉包將數據從一個ViewController傳輸到另一個

[英]How to transfer data from one ViewController to another through closure

我有2個viewController。 我從一個viewController過渡到另一個viewController,然后執行操作,當我返回時,我必須將數據傳遞給第一個viewController,但是前提是要通過閉包來完成此操作

例如ViewController 2中的代碼

 var string: ((String) -> Void)?
@IBAction func action(_ sender: Any) {
    strii?("Lol")
    dismiss(animated: true)
}

首先給閉包起個好名字

var callback : ((String) -> Void)?

IBAction調用它

@IBAction func action(_ sender: Any) {
    callback?("Lol")
    dismiss(animated: true)
}

在第一個控制器中,例如在prepare(for segue指定閉包

let destinationController = segue.destination as! MyGreatController
destinationController.callback = { string in
   print(string)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM