0

I want to display application delegate message such as "Application became active" (This is called when -applicationDidBecomeActive:application is called) on Window.

One way is to use notification center like below:

AppDelegate.m

NSNotification *n = [NSNotification notificationWithName:@"AppBecameActive" object:self];
[[NSNotificationCenter defaultCenter] postNotification:n];

ViewController.m

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(showMessageAppBecameActive) name:@"AppBecameActive" object:nil];

This way is only way to show application delegate message ? Or, is there any other way such as property to look current view controller instance ?

Thank you for your kindness.

Feel Physics
  • 2,783
  • 4
  • 25
  • 38
  • I don't want to use **notification center** because it make code complex. If there is some way such as some property to use, I want to use it. – Feel Physics Aug 27 '12 at 03:55
  • 1
    My take is that in the general case, using properties will make your code far more complex than using `NSNotificationCenter`. But you could always add a `ViewController` property to `AppDelegate` and send it a message directly. – Carl Veazey Aug 27 '12 at 05:06

2 Answers2

0

If you have access to the ViewController from your appDelegate. (I mean like @property or instance is reside in it) you can straight away send a message. If you do not have such access to it. write key value observer for a single ton and let your viewController receive the change.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
-1

Register below code in your viewController which wants notification.

[[NSNotificationCenter defaultCenter]addObserver:self
                                         selector:@selector(yourMethod)
                                             name:UIApplicationDidBecomeActiveNotification
                                           object:nil];

iOS framework posts a notification when your app become active, if you will register via above way, you can handle the notification in registered method (In this case yourMethod).

Apurv
  • 17,116
  • 8
  • 51
  • 67