0

I am creating an alert using UIAlertController and I would like the alert to display on whichever view controller is currently being displayed. This application has about 30 different view controllers and I need the alert to display on whichever view is currently being displayed. Does anyone have a suggestion to do this in the easiest manner.

I am using NSTimer to call a function on one of the views every 30 seconds, looking for a response from a SOAP service. I would like to be able to know which view is currently in use and display the alert if there is a response from my SOAP service.

D. Rock
  • 1
  • 3

2 Answers2

0

You probably want the code that is polling for the SOAP service to generate a broadcast notification, and each of your viewControllers would be observing for that notification. It's possible that all of the viewControllers would get the notification at the same time, but I suspect that only the foreground viewController could act on it (or, you need some way for the viewController to know that it is the active viewController and if not the active one, ignore the notification). Or, you could do the addObserver when the viewController appears, and remove the observer when the viewController disappears.

See this post:

Send and receive messages through NSNotificationCenter in Objective-C?

Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • Thank you Michael. This is exactly what I needed. I gave you an upvote but I do not have enough rep for it to post. – D. Rock May 14 '19 at 23:21
0

self.window?.rootViewController?.navigationController?.topViewController return top view controller on the stack

arts
  • 118
  • 4