0

Below is what I have.

MainViewController.m

- (IBAction)sideMenuAction:(id)sender {
    NSLog(@"login==sideMenuAction");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowMySideMenuNotification" object:self];
}

NotificationListener.m

-(void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}

-(void) adjustShowMenu {
    NSLog(@"notification adjustShowMenu=");
}

Now when I click side menu button in MainViewController, what I was expecting is call adjustShowMenu from NotificationListener once, however it is called twice.

Below is the NSLog for the same.

2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=

What I was expecting is

2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=

Any idea what is going wrong?

Note: I also tried in viewDidAppear instead of viewDidLoad, but its giving same result.

When I searched online, many answers asked to removeObserver. I did same, but still twice notification is getting called.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 2
    Are you sure you have only one instance of NotificationListener? I suspect there must be one more – Andrey Chernukha Jan 20 '15 at 09:34
  • 1
    This is not how you add / remove an observer. You need to add it when you want it and remove it when your done, for example, add it in `viewWillAppear` and remove in `viewWillDisappear` or `viewDidLoad` / `dealloc` – Simon McLoughlin Jan 20 '15 at 09:34
  • Hi Please follow this answer it solved my problem. https://stackoverflow.com/a/7751272/5581345 – Arshad Shaik May 22 '19 at 03:25

1 Answers1

0

As per answer here, I make changes as below and its working fine now.

-(void) viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustShowMenu) name:@"ShowMySideMenuNotification" object:nil];
}

-(void) viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ShowMySideMenuNotification" object:nil];
}
Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • This is a better way of dealing with it because it would work when further view controllers are called from this one AND when this view controller, which is invisible in the background but still around, does not want these notifications while invisible. If the view controller still needs the notifications while not visible but active, then your suggestion would not work. BUT: how does this solve his actual problem of the selector beeing called twice? – Hermann Klecker Jan 20 '15 at 10:16
  • @HermannKlecker : bcz I had one more view controller (language) before coming to this view controller (login)... 2 vc, so 2 notifications – Fahim Parkar Jan 20 '15 at 11:05