How to pass data from child to parent view controller? in swift

I usually use closures for this purpose. Much simpler and less verbose than delegates:

class MainViewController: UIViewController {

    func showChildViewController() {
        guard let vc = storyboard?.instantiateViewControllerWithIdentifier("ChildViewController") as? ChildViewController else {
            return
        }
        vc.didSelectItem = { [weak self](item) in
            if let vc = self {
                // Do something with the item.
            }
        }
        presentViewController(vc, animated: true, completion: nil)
    }

}

class ChildViewController: UIViewController {

    var didSelectItem: ((item: Item) -> Void)?

    @IBAction func buttonPressed() {
        didSelectItem?(item: <#your item goes here#>)
    }

}

  1. My first preference must be Custom Delegate which is faster and perfect. (If you can use closures as callbacks then that can also be a good option. Choosing delegate to explain using your code a bit.)

  2. Please avoid using NSNotificationCenter as much as you can because there are so many thing you have to deal with when using this and it is little slower than delegates. You may easily get into bugs with that too.

Now here is my code.

1. Child ViewController Setup.

   //  TrendingProductPageViewController.swift
   //  buddyiOSApp
   //
   //  Created by Tuhin Samui on 5/21/16.
   //  Copyright © 2016 Buddy. All rights reserved.
   //

    import UIKit

    protocol TrendingProductsCustomDelegate: class { //Setting up a Custom delegate for this class. I am using `class` here to make it weak.
        func sendDataBackToHomePageViewController(categoryToRefresh: String?) //This function will send the data back to origin viewcontroller.
    }


    class TrendingProductPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NetworkReachabilityDelegate {

        @IBOutlet weak var productListTableView: UITableView! //Having a tableview outlet here from storyboard itself. BTW not going to explain with tableView delegate and datasource, Sorry..:(

        weak var customDelegateForDataReturn: TrendingProductsCustomDelegate? //Please use weak reference for this to manage the memory issue.


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            let rowNumberFromTable: Int = indexPath.row //Getting the index of the selected cell.
            let dataToSendBack = moreMenuTableData[rowNumberFromTable] as! String //This is an array which contains the data for the tableview. Getting the exact data which is selected on the table.
            customDelegateForDataReturn?.sendDataBackToHomePageViewController?(dataToSendBack) //Now sending the selected data back to parent viewController using the custom delegate which I made before.                 presentingViewController?.dismissViewControllerAnimated(true, completion: nil) //Dismissing the viewController here.
    }

2. Parent ViewController code here.

class HomePageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, TrendingProductsCustomDelegate, UINavigationControllerDelegate{ //Adding the protocol here as `TrendingProductsCustomDelegate`

@IBAction func topTrendingProductsBtnAction(sender: UIButton) { //Normal IBAction of UIButton as you are using.
        let trendingProductsPageForAnimation = storyboard!.instantiateViewControllerWithIdentifier("showTrendingProductpage") as! TrendingProductPageViewController //You can understand this right. Same as yours.
        trendingProductsPageForAnimation.customDelegateForDataReturn = self //Setting up the custom delegate for this class which I have written on the presenting class.
        trendingProductsPageForAnimation.modalPresentationStyle = UIModalPresentationStyle.FullScreen
        presentViewController(trendingProductsPageForAnimation, animated: true, completion: nil) //Same as yours.
    }


func sendDataBackToHomePageViewController(categoryToRefresh: String?) { //Custom delegate function which was defined inside child class to get the data and do the other stuffs.
        if categoryToRefresh != nil {
            print("Got the data is \(categoryToRefresh)")
        }
    }

}

Hope this helped. Sorry for any mistake.


You can use protocols/delegate

Here is a very very straightforward explanation, no bs: https://www.youtube.com/watch?v=guSYMPaXLaw

Or in your situation you can also use NSNotificationCenter

You can do something like this:

The "sender" view controller would do

let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName("printValue", object: nil, userInfo: ["value" : "Pass Me this string"])

The receiver view controller then can listen to the notification.

let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: #selector(printValue), name: "printValue", object: nil)

func printValue(notification:NSNotification) {
    let userInfo:Dictionary<String,String> = notification.userInfo as! Dictionary<String,String>
    let item = userInfo["value"]! as String

    print(item,self)
}

There are few ways by which you can implement the callback functionality to pass data.

  • Delegate
  • Using Block CallBack
  • Post Notification

But I would suggest to use delegate which is best way, Post Notification is also a way but I do not want to prefer.