0

suppose I have two viewcontroller called A and B. In my viewcontroller B I have a tableview in it. When I selected a cell in my tableview, I want to pass that information back to A.

I have a dictionary that is of the following:

myData = [String: DataModel]

where DataModel takes the form of

struct DataModel{
    var address = ""
    var name = ""
}

I want to send the selected cell's key in B back to A. How should I go about doing that?

thanks for your help

iamVishal16
  • 1,780
  • 18
  • 40
John Doe
  • 185
  • 2
  • 13

3 Answers3

2
  1. Add this before class BViewController:

    protocol ClassBViewControllerDelegate: class {
        func didSelectTableViewCell(onRow row: Int)
    }
    
  2. Create a delegate property in BViewController:

    weak var delegate: ClassBViewControllerDelegate?
    
  3. Implement tableView delegate method tableView(_:didSelectRowAt:)

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
            let row = indexPath.row
            delegate?.didSelectTableViewCell(onRow: row)
    }
    
  4. Tell ClassAViewController that its conforms to ClassBViewControllerDelegate as such:

    class ClassAViewController: UIViewController, ClassBViewControllerDelegate {
    
  5. Bind ClassAViewController and ClassBViewController at an appropriate place in ClassAViewController such a, for instance, prepareForSegue:sender:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SegueIdentifierXYZ" {
            if let vc = segue.destination as? ClassBViewController {
                vc.delegate = self
            }
        }
    }
    
  6. Use delegate method didSelectTableViewCell(onRow row: Int) of delegate contract ClassBViewControllerDelegate in ClassAViewController:

    func didSelectTableViewCell(onRow row: Int) {
        print("Selected table view row is:", row)
    }
    
naeemjawaid
  • 504
  • 3
  • 10
  • 1
    thanks for the fast response. When I followed your example it didn't print anything. I suspect it may be something about the segue. Do you mind explaining it more to me? – John Doe Aug 06 '18 at 16:07
  • 1
    nvm got it. I took out the segue.identifier part and it worked like a charm. Thanks. – John Doe Aug 06 '18 at 16:19
  • Segue is used when one intends to move to another View Controller, and is using storyboards for views and controllers. In case one is creating ViewController in code, one usually set the delegate property on in it after initializing it, and before presentViewController(ViewController, animated: true, completion: nil) or navigationController.pushViewController(ViewController, animated: true) – naeemjawaid Aug 07 '18 at 12:43
0

Using completion block or by using delegates you can achieve this. Using blocks you can code like this:

In your B VC create one property for completion block.

var cellSelectionCallBackHandler: ((DataModel, Int) -> Void)?

Set the cellSelectionCallBackHandler property from VC A.

VCObjectB.cellSelectionCallBackHandler = { (data, index) in
 // Use your data here
}

From VC B call the completion handler on selecting cell like this

cellSelectionCallBackHandler?(yourData, index)

Any doubt plz comment.

vivekDas
  • 1,248
  • 8
  • 12
0

A delegate method can be used to achieve it. The place/method where you call to dismiss B call the delegate. You can implement this delegate in A.

You can also have a singleton dataHandler class, where you can set and get required properties and can access it from anywhere within your project.

Rizwan
  • 3,324
  • 3
  • 17
  • 38