通过导航后退button传递数据

我在这种情况下:

IMG1

我从进度表传递4个数组到详细的练习使用准备赛格,它工作正常! 当我试图将数据从细节练习控制器传回到进度表控制器时,问题就开始了。 我想使用默认的导航返回button返回到父视图。 其实我使用这个代码,但它不工作,以及从孩子传递到父视图的数据,但我看不到进展表中的结果,似乎我需要刷新,但我也尝试了reloadData在viewDidLoad之后,它不起作用。 任何build议? 谢谢。

override func viewWillDisappear(animated : Bool) { super.viewWillDisappear(animated) if (self.isMovingFromParentViewController()){ print("n'drio") let historyView = self.storyboard!.instantiateViewControllerWithIdentifier("historyView") as! HistoryTableViewController historyView.isFirstTime = false historyView.arrayData = arrayDataDetails historyView.arrayRipetizioni = arrayRipetizioniDetails historyView.arrayPeso = arrayPesoDetails historyView.arrayRecupero = arrayRecuperoDetails historyView.tableView.reloadData() } } 

    当您按下后退button时,导航控制器将调用navigationController(willShowViewController:)以便您可以使用navigationController(willShowViewController:)数据传递回您的初始视图控制器。 一个例子如下所示:

    使用UINavigationControllerDelegate

     class DetailsViewController: UIViewController, UINavigationControllerDelegate { // ^ var data: [String] = [] // Important! override func viewDidLoad() { super.viewDidLoad() navigationController?.delegate = self data = ["data has changed!"] } } 

    Swift 2:

     extension DetailsViewController: UINavigationControllerDelegate { func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) { if let controller = viewController as? ProgressTableViewController { controller.data = data // Here you pass the data back to your original view controller } } } 

    Swift 3:

     extension DetailsViewController: UINavigationControllerDelegate { func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { (viewController as? ProgressTableViewController)?.data = data // Here you pass the to your original view controller } } 

    在这个例子中,键是使用UINavigationControllerDelegate并设置导航控制器的委托(在这种情况下它是self )。 完成后,您可以使用后退button将数据发送回您的初始视图控制器。

    我个人更喜欢使用我的数据类:

    为您的数据使用自定义类:

     class Data { var array: [String] = [] } 

    进度视图控制器:

     class ProgressTableViewController: UITableViewController { var data = Data() override func viewDidLoad() { super.viewDidLoad() data.array = ["some data"] } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) tableView.reloadData() } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.array.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() cell.textLabel?.text = data.array[indexPath.row] return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "exerciseSegue", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "exerciseSegue" { let destination = segue.destinationViewController as! DetailsViewController destination.data = data } } } 

    详细视图控制器:

     class DetailsViewController: UIViewController { var data = Data() override func viewDidLoad() { super.viewDidLoad() data.array = ["data has changed!"] } } 

    在最后一个例子中,你不必担心传递数据。 无论何时更改数据,使用相同类的控制器都将进行更改。

    通常,协议和委托用于在屏幕之间来回传递数据。

     // Define a delegate that is known to both view controllers protocol DetailsExerciseDelegate { func detailsWillDisappear(...); } class DetailsExerciseViewController { // Accept the delegate as a property on the details view controller var delegate : DetailsExerciseDelegate override func viewWillDisappear(animated : Bool) { super.viewWillDisappear(animated) // When you want to send data back to the caller // call the method on the delegate if let delegate = self.delegate { delegate.detailsWillDisappear(/* your data in one or more parameters */) } } } // Implement the delegate by adding the required function class ProgressTableViewController: DetailsExerciseDelegate { ... func detailsWillDisappear(...) { // When the child calls the function, update the screen historyView.isFirstTime = false historyView.arrayData = arrayDataDetails historyView.arrayRipetizioni = arrayRipetizioniDetails historyView.arrayPeso = arrayPesoDetails historyView.arrayRecupero = arrayRecuperoDetails historyView.tableView.reloadData() } override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) { if segue!.identifier == "DetailsExcercise" { // And finally, when launching the child view, // make sure to set the delegate. let viewController = segue!.destinationViewController as DetailsExerciseViewController viewController.delegate = self } } } 

    话虽如此,单击后退button时尝试保存数据似乎并不标准。 你确定你不想在“完成”上做这个吗?

    更简单的解决scheme,您可以使用self.navigationController?.viewControllers来访问以前的视图控制器,如下所示:

     let vcsCount = self.navigationController?.viewControllers.count self.navigationController?.viewControllers[vcsCount! - 2].updateData 

    updateData是在前一个视图控制器中要更新的数据成员