0

So I have two view controllers. One has autocomplete for location search, and all it does is let the user use the google places api to get an address. The other lets the user do a keyword search and actually displays results in a table view(with a custom uitablecell).

I want to make it so that the I can take the address from one view controller, execute a search and use the code that draws the table in the other controller to draw my results.

In other words, I'm looking for a way for one view controller to trigger a message and the other view controller to listen.

Is there a way to do this?

praks5432
  • 7,246
  • 32
  • 91
  • 156

1 Answers1

1

When there are more than one receiver, use Notifications. We can set only one delegate.

When to use NSNotificationCenter Checklist:

You need a one-to-many relationships. You need few observers to react on a particular notification. Example: reachability notifications. When your network reachability changes, e.g. wi-fi becomes unavailable, all of the objects subscribed to this type of notifications will receive them and can process accordingly. By design you encourage loose coupling. In the example above, producer that sends a ‘reachability changed’ notification doesn’t know anything about possible observers of this notifications. There could be few of them or none. Same true for observers, they don’t need to know anything about producers of this notification.

When to use Delegates Checklist:

Delegates should always be used only for one-to-one relationships. Use delegates if you encourage tight coupling. Bear in mind, by using delegates you create more interdependency between objects and have more coordination with information flow. A very good example of delegates would be UITableView. UITable ViewDelegate encourages more information flow and creates more interdependency between view controller and table view.

Here is what you need
Notification or Delegate
link 2

Community
  • 1
  • 1
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41