1

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

CGR
  • 370
  • 1
  • 4
  • 18

1 Answers1

1

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
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thank you, hmmm Indeed I'm using `UIViewController+JASidePanel.h` I put that `if` statement inside `ViewDidLoad` of `myViewController.m` but the code inside this `if` it's also being called twice :( – CGR Dec 06 '16 at 00:41
  • Do you mean both the side and center view controller are == self.sidePanelController? That can't be the case. – danh Dec 06 '16 at 00:42
  • Both have the same class `myViewController.m` in their `Storyboard` but a different `Storyboard Id`, `centerViewcontroller` and `leftViewController` – CGR Dec 06 '16 at 00:47
  • The first call is when `centerView` is displayed and the second call occurs when I open the left pannel – CGR Dec 06 '16 at 00:51
  • 1
    Sure. Same class, but different *instances*. We're comparing to self, and only one of those "selves" is in the center position. Since there are two instances of a vc, you *will* see viewDidLoad called twice, but only one of them should satisfy the condition. – danh Dec 06 '16 at 00:52
  • 1 for explanation :) danh, but both are satisfying the condition :s – CGR Dec 06 '16 at 00:58
  • There is also a warning: `Comparison of distinct pointer types ('JASidePanelController *' and 'myViewController*')` – CGR Dec 06 '16 at 01:04
  • Ah. I'm reading the code in that library now. The sidePanelController property answers a *container vc*. The container provides handles to the center left and right vcs. Please see edit. (I only read the extension header before, and guessed -- incorrectly -- what that extension property meant). – danh Dec 06 '16 at 01:10