0

I am pretty sure this question has been asked and answered, but I am not sure what I am doing wrong that I am not being able to get the method called from NSNotificationCenter. I have two classes : Sign In Class And Calendar View Class I want to pass single user credential from SignIn to Calendar. I have put the Calendar in SWRevealController. NSNotification is being called but the trigger method is not called for some reason.

Sign In

- (IBAction)signIn:(id)sender {
    
    _passWord = _password.text;
    NSLog(@"%@ %@",_userName,_passWord);
    [[NSNotificationCenter defaultCenter] postNotificationName: @"Password" object: _passWord];
   [self performSegueWithIdentifier:@"signIn" sender:self];
    
}

Calendar

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *ppnImage = [UIImage imageNamed:@"PPNImage.png"];
    NSLog(@"came here");
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(triggerAction:) name:@"Password" object:nil];
    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.sidebarButton setTarget: self.revealViewController];
        [self.sidebarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }
    events = [[NSMutableArray alloc]init];
    [_indicator startAnimating];
    self.tableView.delegate = self;
    //Creating a background queue
    web = [WebRequests sharedInstance];
    web.delegate = self;
    [web loginView];
    
}

-(void) triggerAction: (NSNotification *) notification {
    NSLog(@"Does not call this");
}

Here

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

is called BUT the below method is not called

-(void) triggerAction: (NSNotification *) notification
 {
    NSLog(@"Does not call   this");
}

Really appreciate if somebody can suggest me what am I doing wrong

Community
  • 1
  • 1
Rosh
  • 169
  • 16
  • You are posting the notification long before the next view controller registers to be told about such notifications. Only currently registered observers are told about a given notification. Why are you using a notification for this anyway? Why don't you pass the value directly to the view controller you are presenting via the segue? – rmaddy Nov 02 '15 at 23:36
  • Since I am using SWRevealViewController , the first view has to be frontal view so I cannot directly pass the value from SignInViewController to CalendarViewController. I have not worked a lot with NSNotification center but everybody was suggesting NSNotification is the best way to move strings from one ViewController to another. Could you please explain on what exactly currently registered observer mean? I am noob on this. – Rosh Nov 02 '15 at 23:40
  • Notification center is fine but only if your desired class is already an observer at the time you post the notification. – rmaddy Nov 02 '15 at 23:41
  • But it seems like CalendarView is observer for postNotifiction:@"Password". What am i missing and what could I do to fix it? – Rosh Nov 02 '15 at 23:42
  • But a breakpoint in the line that adds the observer and on the line that posts the notification. Now run your app. Which breakpoint is hit first? – rmaddy Nov 02 '15 at 23:44
  • addObserver is called before postNotification. – Rosh Nov 02 '15 at 23:50
  • That's good. Then `triggerAction:` should be called when you post the notification. – rmaddy Nov 02 '15 at 23:51
  • Thats the issue, triggerAction: is not even being called at all. It doesn't hit that breakpoint. :( – Rosh Nov 02 '15 at 23:53

0 Answers0