In my iOS app I have the ability to switch themes, in order to switch the theme across all views at once any visible view is subscribed to a NSNotificationCenter notification called "updateTheme" called like this:
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateTheme" object:self];
But when a I am in a view with a custom collection view cell the app crashes upon that cell receiving the notification. This is all the code in the cell:
- (void)didMoveToSuperview {
if (!isNew) {
isNew = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheme) name:@"updateTheme" object:nil];
[self updateTheme];
}
}
- (void)updateTheme {
[UIView animateWithDuration:.5 animations:^{
self.titleLabel.textColor = [Colors boldText];
self.divider.backgroundColor = [Colors darkBorder];
} completion:^(BOOL finished){}];
}
When I comment out the line in which the cell subscribes to the notification then the app runs fine. Otherwise it crashes with this error
-[__NSCFType updateTheme]: unrecognized selector sent to instance 0x1775d5d0
I can't figure out why this is happening and any help or suggestions would be appreciated.
As was pointed out by matt, I was not removing the cell as an observer upon deallocation and subsequently when the notification was triggered the object was gone so the app crashed. To fix it I added this:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}