How do I pass a variable from detail view controller to master view controller in Master-Detail iOS app?

In a Master-Detail iOS app, how do I pass a variable from the detail view controller to the master view controller. There is not an unwind or prepare callback method when transitioning from detail view controller to master view controller.

Accepted Reply

What I had to do:

- master view contains a list of "pages"

- when selecting, opens in detail

- once detail is completed, I needed to mark the item in the master list as complete.


Is your use case similar ?

I will complement my example with the passing of a value from detailView.


What I did in this case was to use delegation.


Defined a protocol


protocol DetailViewControllerDelegate  {
    func validateSection(num: Int, valid: Bool)     // num being the item in the list to update

    func passValueFromDetail(value: Int)
}


have master conform to it, and declare a var to get the transmitted value


class MasterViewController: UITableViewController, DetailViewControllerDelegate {
     var valueToUse: Int?


Implement the func in Master


    func validateSection(num: Int, valid: Bool) {
  
        // update dataSource with the new value for item num
        tableView.reloadData()
        // Do what else is needed
        }
    }


    func passValueFromDetail(value: Int) {

        valueToUse = value
       // Do what you need with valueToUse: update a field…
        }
    }


Still in Master, define the delegate in prepare

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Do what's needed

      controller.delegate = self
}


In detailViewController, create a delegate


class DetailViewController: UIViewController  { 

    var delegate: DetailViewControllerDelegate?

// And call the delegate where I need to

   delegate?.validateSection(num: someNum, valid: true) 
   delegate?.passValueFromDetail(value: 100)     // 100 as an example or any value you want

Replies

What I had to do:

- master view contains a list of "pages"

- when selecting, opens in detail

- once detail is completed, I needed to mark the item in the master list as complete.


Is your use case similar ?

I will complement my example with the passing of a value from detailView.


What I did in this case was to use delegation.


Defined a protocol


protocol DetailViewControllerDelegate  {
    func validateSection(num: Int, valid: Bool)     // num being the item in the list to update

    func passValueFromDetail(value: Int)
}


have master conform to it, and declare a var to get the transmitted value


class MasterViewController: UITableViewController, DetailViewControllerDelegate {
     var valueToUse: Int?


Implement the func in Master


    func validateSection(num: Int, valid: Bool) {
  
        // update dataSource with the new value for item num
        tableView.reloadData()
        // Do what else is needed
        }
    }


    func passValueFromDetail(value: Int) {

        valueToUse = value
       // Do what you need with valueToUse: update a field…
        }
    }


Still in Master, define the delegate in prepare

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Do what's needed

      controller.delegate = self
}


In detailViewController, create a delegate


class DetailViewController: UIViewController  { 

    var delegate: DetailViewControllerDelegate?

// And call the delegate where I need to

   delegate?.validateSection(num: someNum, valid: true) 
   delegate?.passValueFromDetail(value: 100)     // 100 as an example or any value you want

Hey thanks.