0

I am trying to use postnotification but not able to implement it properly. This is what I have :

In ViewControllerOne.m

NSLog(@"PostNotification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"Connectivity" object:nil];

In ViewControllerTwo.m

- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSLog(@"Added Obeserver");

    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(connectedTo:) name:@"Connectivity" object: nil];

}

-(void)connectedTo:(NSNotification *)notification
{
    m_connectivity = @"Connected";
}

It seems that connectedTo function is not being called. This is because:

In another part of the code:

if ([m_connectivity isEqualToString:@"Connected"])
{
       NSLog(@"Connected");
}
else
{
     NSLog(@"NotConnected");
}

Not sure what my mistake is. Nee some guidance... Thanks..

EDIT:

ViewControllerOne.m is a class that other viewcontrollers subclass upon. It checks connectivity and when connected, I need to inform the other viewcontroller(ViewControllerTwo) that i am connected and take necessary action based on connectivity. So when connectivity changes, the notification will get posted but the viewcontroller might not been initialized at that point...

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • did you import `ViewControllerOne.m` in target class `ViewControllerTwo.m` – Anoop Vaidya Mar 12 '13 at 17:18
  • 1
    Is it possible the notification is being posted **before** `viewDidLoad` is being called? – Mike D Mar 12 '13 at 17:20
  • 2
    @AnoopVaidya, there's no need to do that. – rdelmar Mar 12 '13 at 17:20
  • @MikeD it is possible... – lakshmen Mar 12 '13 at 17:20
  • Where in ViewControllerOne are you posting the notification? – rdelmar Mar 12 '13 at 17:20
  • @rdelmar not in viewdidload but a specific function that i have written... – lakshmen Mar 12 '13 at 17:21
  • I think what @rdelmar means what is the flow. If the notification is being post before `viewDidLoad` is called, then that would be why `connecteddTo:` is not being called. – Mike D Mar 12 '13 at 17:23
  • @MikeD how to solve it then? – lakshmen Mar 12 '13 at 17:24
  • 1
    'in a function" is not very helpful. The order that these things happen is important. What function? When in the life cycle is it called? How and when do you instantiate the second controller? – rdelmar Mar 12 '13 at 17:24
  • I am not sure but few times i faced the problem and #import worked out. Might be a luck that time led to wrong perception now :( – Anoop Vaidya Mar 12 '13 at 17:24
  • 1
    @AnoopVaidya, yes that absolutely is a wrong perception. Notifications are completely anonymous -- neither the sender nor the receiver needs to know anything about the other. – rdelmar Mar 12 '13 at 17:28
  • Thanks to SO, today this is my 2nd good lesson for today :) – Anoop Vaidya Mar 12 '13 at 17:29
  • 1
    Look, this is a very easy problem to diagnose -- put a log in where you post the notification, and another where you register to receive it. See which gets logged first. – rdelmar Mar 12 '13 at 17:30
  • @rdelmar the function is a private function i have written and can't share it. That is why i said a specific function... – lakshmen Mar 12 '13 at 17:31
  • The suggestion by @rdelmar still applies. – Mike D Mar 12 '13 at 17:32
  • I added two nslogs: one called PostNotification and another called Added Oberserver.. only postnotification gets called... – lakshmen Mar 12 '13 at 17:42
  • You should post the where ViewControllerTwo is instantiated, and where the notification is posted, along with a description of the flow. – Mike D Mar 12 '13 at 17:44
  • Assuming you put that log for the add observer in the viewDidLoad method where you register the observer, that means ViewControllerTwo's view is never being loaded. – rdelmar Mar 12 '13 at 17:47
  • So, it looks like your problem has nothing to do with NSNotifications. Your controller two's view is not even being loaded. So, how are you instantiating ViewControllerTwo? If it hasn't been put on screen by the time you post the notification, then nothing is going to work. You could try moving the addObserver code to either awakeFromNib or initWithCoder (if you're using a storyboard), and see if that works. – rdelmar Mar 12 '13 at 17:58
  • It sill comes down to understanding the life cycle of view controllers and tracing the application flow. – Mike D Mar 12 '13 at 18:01
  • I understand and most importantly, thanks for the help. My question will be if the case the viewcontroller did not load up, postnotification does not work.. then how should be a good method to solve this? having global variables/Singleton class? – lakshmen Mar 12 '13 at 18:15
  • Wouldn't a simple solution be to have a singleton _connectivity observer_ that posts notifications whenever your connectivity state changes? Then also have your view controllers check the _connectivity observer_ for the current connectivity state when they are created. – nielsbot Mar 12 '13 at 20:25

2 Answers2

1

Since ViewControllerTwo is a subclass of ViewControllerOne, you could have a method in ViewControllerOne that returns a BOOL based on the connection state. You can call this method in the viewDidAppear method of ViewControllerTwo to check on that state when ViewControllerTwo first comes on screen. You could still use a notification if you want, to update ViewControllerTwo when the connection state changes. Or, you could just call this method whenever you're about to do anything that requires a connection.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks for the help and answer. One more question: what if the viewcontrollerTwo does not subclass viewcontrollerOne? what i mean-> what if they are two separate controllers? how would you tackle this scenario? – lakshmen Mar 13 '13 at 06:12
  • That would depend on how the two controllers relate to each other. For instance, if VC1 creates VC2, then VC1 could set itself as the delegate of VC2, and VC2 could call the delegate method which would return the connected state to VC2. There are various other ways for one view controller to get a reference to another, so it can call methods in that controller, but you'd have to give me more specific details for me to give a more complete answer. – rdelmar Mar 13 '13 at 06:54
0

Have you tried the alternate syntax for posting a notification such as:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"HandleOpenURL" object:nil]];

The postNotification method takes an NSNotification object, not an NSString.

radesix
  • 5,834
  • 5
  • 24
  • 39
  • This is one way. The method described in the question is also correct, it will build the `NSNotification` object. https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html – Mike D Mar 13 '13 at 02:07
  • how does taking a nsnotification object help to solve the problem? can explain to me more... – lakshmen Mar 13 '13 at 02:48