Sending data via notificationcenter in a singleton objective-c

857 views Asked by At

I am sending a username and password to an API and returning an ID if it's correct, or false if it's incorrect. I have a singleton set up to retrieve the data. I am wanting to return the data to my original view controller to use in methods depending on if I have false or an ID. But for some reason, it is not sending. Here is my code so far:

original view controller:

- (IBAction)btnLogIn:(id)sender
{
//get username and password from user, send to singleton
[[APIManager sharedManager] validateLoginWithUsername:[_lblUserName text] andPassword:  [_lblPassword text]];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //set up notification center, invoke login_success or login_fail methods
[nc addObserver:self selector:@selector(login_success) name:@"loginValid" object:nil];
[nc addObserver:self selector:@selector(login_fail) name:@"loginInvalid" object:nil];

singleton:

  if ([result isEqualToString:(@"false")])
{
 [[NSNotificationCenter defaultCenter] postNotificationName:@"loginInvalid" object:nil];
}

else
{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"loginValid" object:nil];
}
1

There are 1 answers

0
picciano On BEST ANSWER

It appears that you are calling addObserver after you call validateLoginWithUsername. It is likely that the notification is being posted before you are listening for it.

Call addObserver first. You should add some logging to verify this.