lakshmen
lakshmen

Reputation: 29064

not able to get postnotification to work properly

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...

Upvotes: 0

Views: 160

Answers (2)

radesix
radesix

Reputation: 6262

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.

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

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.

Upvotes: 1

Related Questions