簡體   English   中英

如何將數據傳回Swift中的第一個視圖控制器?

[英]How to pass data back to the first view controller in Swift?

我有兩個視圖控制器,我有這個代碼在兩個視圖之間導航

ViewController:
func goToSecondViewController() {
  let aa = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
  self.navigationController.pushViewController(aa, animated: true)
}

/

SecondViewController:
func goToFirstViewController() {
  self.navigationController.popToRootViewControllerAnimated(true)
}

如何在彈出之前將一些數據發送到第一個視圖?

如果A是具有以下聲明的視圖控制器:

class A : UIViewController {
   var data: String!
}

然后,只要有A實例,就可以直接設置數據屬性:

let a = A() // assuming you've defined the init method
a.data = "hello"

編輯

如果你想在彈出之前發送數據,你可以這樣做:

func goToFirstViewController() {
  let a = self.navigationController.viewControllers.first as! A
  a.data = "data"
  self.navigationController.popToRootViewControllerAnimated(true)
}

(還沒編譯上面的內容)

暫無
暫無

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

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