![](/img/trans.png)
[英]segue and transfer data from viewcontroller to viewcontroller to another 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.