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?