0

I have several view controllers each of them has a label or a button. I want to change visible view controller button's or label's text from the AppDelegate. I know that it is a bad practice but I in the AppDelegate runs a background thread which looks fow new images for user on the server and if there are ones it must update new images counter in the label on the navigation bar of the current dispalyed view.

My idea was to use in the AppDelegate.m next code: (ControllerWhichLabelIWantToChange*) self.navigationControllerClass.visibleviewcontroller. ... But here I can't see a label or a button for which I define a property in ControllerWhichLabelIWantToChange.

So the question how can I access elements of different view controllers from the AppDelegate and change them?

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102

1 Answers1

4

One suggestion, which might be better for you, is to use NSNotificationCenter to send notifications on various changes. Any view wishing to refresh themselves may respond to such notifications. This is a common practice. Say, for example, you are processing theme data in the background, and you've got 20 live views that require theming, you could post a notification when your background process has completed, and all views that are observing the notification will be notified, and they can update themselves.

Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • Thanks. As i understand you correctly I need to use addObserver:visibleviewcontroller? Can you please give some code samples – MainstreamDeveloper00 Feb 07 '13 at 16:51
  • Close, but no cigar. Check out the `addObserverForName:object:` method of the [NSNotificationCenter](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html). Example: [Send and receive messages through NSNotificationCenter in Objective-C?](http://stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c) – Jeremy Feb 07 '13 at 16:55