سؤال

I have a two tabs on a UITabBarController and I have registered both with NSNotificationCenter, my problem is I am not getting notifications on the hidden tab (i.e. its not called viewDidAppear: yet). My thinking is that controllers not on screen (i.e. hidden) do not respond to NSNotifications. I can do things a different way thats not a problem, but I just want to verify why the hidden tab is not getting the notification in case I am missing something else and it should actually be working ...

EDIT:

@Fab1n pointed me in the right direction, I had mistakenly used viewWillDisappear: to remove the observer, so when the view disappeared is was no longer listening out for notifications. I will move it to dealloc.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self];
}

Changed to:

- (void)dealloc {
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter removeObserver:self];
}

Much appreciated.

هل كانت مفيدة؟

المحلول

To stay safe: Register your NSNotification in -init or (with IB) -initWithNibName:

A controller of a UITabbarController is initialized with init when you set the controllers property of the UITabbarController.

viewWillAppear and viewDidAppear are called each time you make one of the controllers visible by tapping on a tabbar icon.

Solution:

  1. register your NSNotification by overriding -init in each of your 'sub'-controllers
  2. unregister the notification in -dealloc (don't call [super dealloc] with ARC!!!)

Now everything works fine!

نصائح أخرى

As far as i know any tab bar element is lazy loaded despite the first one. They get loaded the first time you tap on the representing tab bar item (or by accessing their view property programmatically). Afterwards they can receive any message you want even if you select another tab item.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top