I subscribe to notifications in the SettingsController controller

 - (void)viewDidLoad { [super viewDidLoad]; self.title = LocalizedString(@"titleLabelSettings", @""); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabels) name:kNotificationLanguageChanged object:nil]; } - (void)updateLabels { self.title = LocalizedString(@"titleLabelSettings", @""); } 

and move on to the next controller

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.navigationController pushViewController:[[ChangeLanguageController alloc] init] animated:YES]; } 

The dealloc method is not called on the SettingsController

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationLanguageChanged object:nil]; NSLog(@"dealloc SettingsController"); } 

I ChangeLanguageController notification from the ChangeLanguageController controller and the SettingsController does not receive it. Why?

 [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLanguageChanged object:nil userInfo:nil]; 
  • is everything called normal when the controller is on screen? - Max Mikheyenko 2:53 pm
  • and both controllers see the same value for kNotificationLanguageChanged? - Max Mikheyenko 8:50 pm
  • When the controller is on the screen and a notification is sent, the selector is executed. AppDelegate, too, always handles the notification - Alexey Alybin
  • And where do you call the notification from the ChangeLanguageController controller? Your code is fine. Put the breakpoint in the updateLabels method and in the dealloc method. Perhaps there is a problem in the ChangeLanguageController controller, or you see no changes because the self.title property is the same in both methods, but the method is being called. - Artur Mirrov
  • set breakpoints - dealloc is not called, and the selector is also not called - Alexey Alybin

1 answer 1

Somehow it’s not right that you subscribe to viewDidLoad (it can be invoked many times), and you unsubscribe in dealloc. If he should always listen, transfer the subscription to the controller's initWithNibName: method.

In your case, you can remove the notification altogether and call updateLabels from the SettingsController viewWillAppear method.

PS: check all the place where there is removeObserver, most likely you somewhere unsubscribe yourself.

  • Starting with ios6, calling viewDidLoad more than once is almost impossible. Even with a lack of memory, the layer bitmap (layer) is unloaded from memory, and the UIView and CALayer objects are preserved intact. This outcome is so unlikely that even if you wish, it is very difficult to do. Therefore, subscribing to this method is quite normal. - Artur Mirrov 3:54 pm
  • it's still hard to imagine that you need to track events from the loading view to the end of life. Most likely you need to keep track of your whole life or all the time displayed on the screen. - Valentine
  • Checked on another unique identifier that is not used anywhere else. Conclusion - the controller does not receive notifications if it is not on the active screen. I tried to make a subscription in the init method - the result is the same (the code is called - checked) - Alexey Alybin