ref – http://stackoverflow.com/questions/8200775/when-to-unsubscribe-from-a-nsnotification-in-a-uiview
In your UIView custom class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
- (void)didMoveToWindow { if (self.window) { NSLog(@"Added to a window, similar to -viewDidLoad."); // Subscribe to notifications here. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(topicAndEntry:) name:INSERT_TOPIC_ENTRY object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(topicAndEntry:) name:INSERT_REQUIRED_KEY_VALUE object:nil]; } } - (void)willMoveToWindow:(UIWindow *)newWindow { if (newWindow == nil) { NSLog(@"Will be removed from window, similar to -viewDidUnload."); // Unsubscribe from any notifications here. [[NSNotificationCenter defaultCenter] removeObserver:self name:INSERT_TOPIC_ENTRY object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:INSERT_REQUIRED_KEY_VALUE object:nil]; } } |