How to pass data between two different (unrelated) view controllers? From my understanding of delegates, a relationship of some sort is required (eg. Prepare for segue, etc). I have a separate view controller that displays search results, and I want to trigger a tableview in a different screen with a separate view controller to scroll to that position, and highlight the row. In order to do that, I need to let the table view know what to highlight.
3 Answers
In order to do that, you can use the NSNotificationCenter (Apple Docs)
In short, in the receiving viewController, you declare an observer in the viewDidLoad :
NSNotificationCenter.defaultCenter().addObserver(self, selector: "actOnSpecialNotification", name: "myNotification", object: nil)
and a corresponding function to be called
func actOnSpecialNotification() {
self.notificationLabel.text = "I heard the notification"
}
When you need to send a message to that viewController, in your sending viewController, you send the following message:
NSNotificationCenter.defaultCenter().postNotificationName("myNotification", object: self)
For more information I invite you to check this well made tutorial by Andrew Bancroft: The Tutorial

- 2,790
- 6
- 29
- 33
-
1I had hoped to avoid NSNotificationCenter as I read it's not best practice, but it seems like the only way to do this, so thanks for confirming this for me – user3601148 Feb 17 '16 at 10:48
-
1Also, NSNotificationCenter can be used to send objects, so this was a good solution in the end – user3601148 Feb 17 '16 at 11:53
Besides the already mentioned Notifications you can use the appDelegate to access globals:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let aVariable = appDelegate.someVariable

- 2,139
- 17
- 38
An easier method is to make use of a static member in the consumer controller and then set it in the producer controller. When the consumer controller becomes active you can use the data and clear it so that its not used again the next time. One advantage of this method is that it keeps the data required by a view controller within itself and not distributed across the app such as when using globals.

- 3,671
- 1
- 11
- 15