我想刷新整個頁面控制器在後面按。 我使用代碼瀏覽viewcontroller。返回事件是handel在快速3.0哪裏
我的代碼
let GTC = self.storyboard?.instantiateViewController(withIdentifier: "GoToCart")as! GoToCart
self.navigationController?.pushViewController(GTC, animated: true)
我想刷新整個頁面控制器在後面按。 我使用代碼瀏覽viewcontroller。返回事件是handel在快速3.0哪裏
我的代碼
let GTC = self.storyboard?.instantiateViewController(withIdentifier: "GoToCart")as! GoToCart
self.navigationController?.pushViewController(GTC, animated: true)
使用viewWillAppear
重新加載UI。在使用navigationController?.pushViewController
時,視圖將被保留並存儲在堆棧中。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Reload the UI
}
viewWillAppear
被稱爲第一時間的視圖被顯示,以及當再次顯示的視圖,因此它可以視圖控制器對象的壽命期間被多次調用作爲。當用戶點擊後退按鈕,在選項卡欄控制器中選擇視圖控制器的選項卡時,視圖即將出現時調用該方法。確保在實現的某個時刻調用super.viewWillAppear()
。您可以刷新此方法您的UI
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Reload the UI
}
一個更好的辦法是使用協議
例
調用委託方法在GoToCart:寫以下代碼..
protocol GoCartControllerDelegate
{
func childViewControllerResponse(parameter)
}
class GoToCart:UIViewController
{
var delegate: ChildViewControllerDelegate?
....
}
然後在mainViewController實現協議功能端擴展至協議
class MainViewController:UIViewController,GoCartControllerDelegate
{
// Define Delegate Method
func childViewControllerResponse(parameter)
{
//...here update what you want to update according to the situation
}
}
2重要的事情
導航到GOCART控制器代碼時這樣
let GTC = self.storyboard?.instantiateViewController(withIdentifier: "GoToCart")as! GoToCart
GTC.delegate = self
self.navigationController?.pushViewController(GTC, animated: true)
啪從gocartViewController 這樣的代碼
self.navigationController?.popViewController(animated:true)
self.delegate?.childViewControllerResponse(parameter)
時