2017-07-04 23 views
2

我想刷新整個頁面控制器在後面按。 我使用代碼瀏覽viewcontroller。返回事件是handel在快速3.0哪裏

我的代碼

let GTC = self.storyboard?.instantiateViewController(withIdentifier: "GoToCart")as! GoToCart 
self.navigationController?.pushViewController(GTC, animated: true) 

回答

0

使用viewWillAppear重新加載UI。在使用navigationController?.pushViewController時,視圖將被保留並存儲在堆棧中。

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    // Reload the UI   
} 
0

viewWillAppear(_:)

viewWillAppear被稱爲第一時間的視圖被顯示,以及當再次顯示的視圖,因此它可以視圖控制器對象的壽命期間被多次調用作爲。當用戶點擊後退按鈕,在選項卡欄控制器中選擇視圖控制器的選項卡時,視圖即將出現時調用該方法。確保在實現的某個時刻調用super.viewWillAppear()。您可以刷新此方法您的UI

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    // Reload the UI   
} 
0

一個更好的辦法是使用協議

  1. 從那裏,你想彈回創建協議(GoToCart)
  2. 在GoToCart
  3. 創建委託變量
  4. 在MainViewController中擴展GoToCart協議
  5. 當參考 導航
  6. 參考MainViewController的GoToCart
  7. 定義委託方法在MainViewController
  8. 然後就可以從GoToCart

調用委託方法在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)