-1

When my app will enter foreground, I need to call some methods of my view controller from the app delegate class. How is the best way to realise it?

I try to create a "fake" notification in "applicationWillEnterForeground" method:

[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:nil];

And add observer in view controller:

[self addObserver: self selector:@selector(testMethod) name:@"test" object:nil];

Is this OK? Another method there: Calling UIViewController method from app delegate

Which approach is better and why?

PS Sorry for my bad english.

Community
  • 1
  • 1
  • 1
    possible duplicate of [how to call the method one class to another class in obj c](http://stackoverflow.com/questions/28473878/how-to-call-the-method-one-class-to-another-class-in-obj-c) – jscs Feb 12 '15 at 21:14

2 Answers2

0

Quick Answer:

  1. Yes, that should work with your "fake" notification
  2. But why should you do so? You could let your viewController do all the things without die AppDelegate -> just call your methods, that you want to be done in the 'viewDidLoad'-method of your viewController. (I assume, your viewController is the entry point of your App)
LukeSideWalker
  • 7,399
  • 2
  • 37
  • 45
0

Notification will support many destinations, whereas the answer here only supports one viewController being told what to do.

On the other hand, calling directly through a reference is easier to understand and to debug.

I don't understand why you call it a "fake" notification - this is exactly how that call is meant to be used.

Community
  • 1
  • 1
Bryan
  • 11,398
  • 3
  • 53
  • 78