0

Essentially I have a TableViewController that is populated using JSON. Each row can be identified with the following line in the didSelectRowAt

portfolio.customer

I would like to share the value of the row selected with a few other ViewControllers

But not all of these ViewControllers will open as soon as the user selects the row, in fact some ViewControllers may be opened a few minutes after the cell is selected.

The value of this cell selected will, of course, change every time a new one is selected.

How can I use NSNotifications to pass the value of selectedCustomer to other view controllers.

This is the code in didSelectRowAt:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let portfolio = structure[indexPath.row]

    selectedCustomer = portfolio.customer
}

structure is the structure I am using to decode the JSON values, the customer is a string value.

Here is an example of what I have tried so far:

In didSelectRowAt I added the following NotificationCenter .post:

NotificationCenter.default.post(name: Notification.Name("YOURKEY"), object: nil, userInfo: ["titleKey": selectedCustomer])

In one of the other ViewControllers I added an observer in viewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(self.myFunc(notification:)), name: Notification.Name("YOURKEY"), object: nil)

And the following function to interpret the value passed and print it

 @objc func myFunc(notification: Notification) {

        guard let data = notification.userInfo as? [String: String] else {
            return
        }
        debugPrint("my title \(data["titleKey"])")

    }

The function myFunc fails to print the selected cells value for some reason and I don't understand why?

  • 1
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – dengApro Jun 27 '19 at 19:06
  • Your question is far too broad. Passing / sharing data between view controllers throughout your app is a ***very*** basic and fundamental task, and there are many different ways to do it depending on your specific needs. You really need to do a google search for `swift pass data to view controller` and read up on the different approaches until you understand it and find one that works for you. (Note: Don't post virtually identical questions here on Stack Overflow) – DonMag Jun 27 '19 at 19:24
  • I added more details to my question: I have tried using the NSNotification method but cannot get the value of the row selected to print, I would really appreciate your help –  Jun 27 '19 at 19:34
  • man i told in other question that this is not the right question to do, you're repiting the same question in diferent post, and the task you are asking is not what really you want to do. so if you want to pass data between viewcontroller the answer is allredy answered, but is you want to pass that value to multiple view controller you should ask that precisily. – Yoel Jimenez del valle Jun 27 '19 at 20:13
  • If the other view controller doesn't receive a call, that's because it's not there to listen to calls. There is no point of sending a signal to a view controller that has not even appeared yet. – El Tomato Jun 27 '19 at 20:44

0 Answers0