I'm trying to get the hang of using notifications. In my view controller class, I have a bool isFullScreen. When the value of this bool changes, I want to a notification to be sent to all observing classes. I'm not quite sure how to go about doing this, since a BOOL is not an object. How would I accomplish this?
A notification center can deliver notifications only within a single program; if you want to post a notification to other processes or receive notifications from other processes, use NSDistributedNotificationCenter instead.
An object may therefore call this method several times in order to register itself as an observer for several different notifications. Each running app has a defaultCenter notification center, and you can create new notification centers to organize communications in particular contexts.
Objects register with a notification center to receive notifications ( NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. When an object adds itself as an observer, it specifies which notifications it should receive.
Each running app has a defaultCenter notification center, and you can create new notification centers to organize communications in particular contexts.
[[NSNotificationCenter defaultCenter] postNotificationName:YourNotificationName object:[NSNumber numberWithBool:isFullScreen]]; //YourNotificationName is a string constant
KVO Example:
If you were to do it with KVO, it would be something like the below.... :
[self addObserver:self forKeyPath:@"isFullScreen" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString: @"isFullScreen"]) {
BOOL newValue = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
}
}
//and in dealloc
[self removeObserver:self forKeyPath:@"isFullScreen" ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With