In my Swift project, ViewController #1 has a route of destinations listed in a table. User is driving along and we keep track of where user is using the built-in CLLocation stuff. But user has now navigated away from ViewController #1 to some other portion of the app and is now in ViewController #2. A certain event that I track in ViewController #1 fires (in my example; the user has reached the next destination on his list). I need the app to immediately segue back to ViewController #1 right when that event fires. How can it be sensed when user is in ViewController #2?
Asked
Active
Viewed 92 times
1
-
2You can use `NSNotificationCenter`and push notification and capture this notification all over your app and go from anywhere in your app to viewController#1 or you can show dialog showing that the user is where is needed – Reinier Melian Jun 07 '16 at 21:16
-
Thanks! I'll check that out. – Chaim Friedman Jun 07 '16 at 21:23
1 Answers
1
NSNotificationCenter and an Unwind Segue seems to be the way to go, this way this functionality can be reused, all you would need to do is register for a given notification on the relevant view controllers.
Something along the lines of:
class ViewControllerOne: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
//- your event happens
SomethingHappened {
NSNotificationCenter.defaultCenter().postNotification(CallForUnwindSegue, object: nil)
}
}
class ViewControllerTwo: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
override func viewDidLoad() {
super.viewDidLoad()
//- Subscribe for notifications
NSNotificationCenter.defaultCenter().addObserver(self, #selector(self.doSomething), name: CallForUnwindSegue)
}
//- Unsubscribe for notifications
override func deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func doSomething(notification: NSNotification) {
//Handle your event here.
}
}
There's a good unwind segue article here

Daniel Ormeño
- 2,743
- 2
- 25
- 30
-
Thanks, I'm familiar with the unwind part. I've done that before. The question was being notified of changes and that's where the NSNotiifcation part comes in. Thanks for the code sample - that is exactly what I was looking for. – Chaim Friedman Jun 08 '16 at 12:36
-
-
I don't know why 5 moderators above placed this post on hold as "too broad" though. – Chaim Friedman Jun 08 '16 at 12:41