Pass data through navigation back button

后端 未结 4 1251
一向
一向 2020-12-24 03:10

I am in this situation:

I am passing 4 array from Progress Table to Detail Exercise using prepare for segue and it works fine! The problem begin when I try

4条回答
  •  一向
    一向 (楼主)
    2020-12-24 03:56

    Typically, protocols and delegates are used to pass data back and forth between screens.

    // 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
            }
        }
    }
    

    That being said, it seems non-standard to try to save the data when clicking the back button. Are you sure you don't want to do this on "Done" instead?

提交回复
热议问题