Pass data through navigation back button

后端 未结 4 1253
一向
一向 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 04:00

    When you press the back button, the navigation controller will call navigationController(willShowViewController:) so you can use this to pass the data back to your initial view controller. An example is shown below:

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

    In this example, the key is using the UINavigationControllerDelegate and setting the delegate of the navigation controller (in this case it's self). Having done this, you can send the data back to your initial view controller with the back button.

    Personally I prefer using a class for my data:

    Using a custom class for your data:

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

    Progress view controller:

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

    Details view controller:

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

    In the last example, you don't have to worry about passing around data. Whenever you change the data, the controllers using the same class, will have the changes as well.

提交回复
热议问题