Search code examples
objective-cxcodensnotificationcenterjasidepanels

NSNotificationCenter called twice in ViewDidLoad


I'm using JASidePanels with storyboards in my aplication and also using NSNotificationCenter

The problem is that:

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

It's being called twice inside viewDidLoad one call when the storyboard is going to be displayed CenterViewController and the second one, when I reveal the left panel LeftViewController, I'm using the same class for both, is there a way to stop this?

I've tried the bellow code but is not working,

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:@"leftPanelMsg"
                                                      object:nil];

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

Also I used a bool to exec the code just 1 time, I turn mustRun bool to NO when code was executed for first time (leftPanelMsg), but at the next notification when leftPanelMsg is called again mustRun return it's value to true, don't know why


Solution

  • It looks like that library provides a view controller extension which answers sidePanelController. So your vc's can ask directly...

    #import "UIViewController+JASidePanel.h"
    // ...
    
    if (self.sidePanelController.centerPanel == self) {
        // observe notification
    }
    

    or you can ask:

    if (self.sidePanelController.leftPanel == self) // ... and so on