How to send bool status to NSNotificationCenter?

I am trying to use notifications. In my opinion, the controller class, I have a bool isFullScreen. When the value of this parameter changes, I want the notification to be sent to all watch classes. I'm not quite sure how to do this, since BOOL is not an object. How to do it?

+4
source share
6 answers
[[NSNotificationCenter defaultCenter] postNotificationName:YourNotificationName object:[NSNumber numberWithBool:isFullScreen]]; //YourNotificationName is a string constant 

KVO example:

If you did this with KVO, it would be something like 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" ]; 
+10
source

Just wrap BOOL in NSNumber:

 [NSNumber numberWithBool:myBool] 
+3
source

You can wrap BOOL in NSNumber, for example, bandejapaisa and beryllium. However, to notify observers about changes to a simple property, you better use Key Value Observing (KVO) instead of NSNotificationCenter. You get KVO β€œfor free” as long as you have implemented or @synthesized KVC-compliant access methods. Something like that:

 // In your .h: @interface YourViewController : UIViewController @property (getter = isFullScreen) BOOL fullScreen; @end // In your .m: @implementation YourViewController @synthesize fullScreen; @end // In your observer class(es): // Start observing the viewController for changes to fullScreen (in awakeFromNib, or wherever it makes sense) [self.viewController addObserver:self forKeyPath:@"fullScreen" options:0 context:NULL]; // This method is called when an observed value changes - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == viewController && [keyPath isEqualToString:@"fullScreen"]) { if (self.viewController.isFullScreen) { // Do whatever you need to do in response to isFullScreen being true } else { // Do whatever you need to do in response to isFullScreen being false } } } 

For this to work, you need to make sure that you are really calling setter on the fullScreen property. Therefore, always self.fullScreen = YES [self setFullScreen:YES] instead of fullScreen = YES . Otherwise, the setter method is not called, and KVO does not start.

You should read the KVO documentation. Understanding this is quite fundamental in order to be a good iOS programmer.

+2
source

You can think it over with NSMutableString * like @ "YES" and @ "NO". Then, when you set the line @ "YES", and everything that is observed in KVO will be notified:

 [myStringProperty setString:@"YES"]; 

You must use the value of NSttring setString. This is what actually causes it.

WARNING DO NOT use:

 myStringProperty = @"YES"; 

(This will not result in KVO notifications.)

0
source

object notification object must be the object sending the notification, not NSNumber . This is important so that the observer can observe specific instances, and so that it is obvious to the caller that all values ​​are. Passing data to an object is an easy way to get "no response to selector" failures. Data modification usually occurs in the userInfo dict, but simple BOOLs are usually handled by two notifications. In this case, you will have:

 MYViewControlDidEnterFullScreenNotification MYViewControlDidExitFullScreenNotification 

object must be the appropriate view controller.

Please note that these notifications are extremely clear in their time. Both occur after the state has changed. You can also have equivalent Will notifications. See the list of notifications for NSWindow for a good example of how to do this correctly. Especially notice NSWindowDidEnterFullScreenNotification and its relatives.

You may also be interested in Some Thoughts on NSNotifications .

The comments on KVO are good, and often KVO is a decent way to achieve this. But notifications are also good, and easier to understand and debug than KVO.

0
source

The field of the notification object is intended for sending the sender, and not for additional parm

The correct way is to use userInfo to send a dictionary of key values

for instance

 - (void)postNotificationFullScreen //post notification method and logic { NSString *notificationName = @"applicationFullScreen"; NSString *key = @"fullScreen"; NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:self.fullScreen] forKey:key]; [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dictionary]; } 

And to read it

 NSString *notificationName = @"applicationFullScreen"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(useFullScreen:) name:notificationName object:nil]; - (void)useFullScreen:(NSNotification *)notification //use notification method and logic { NSString *key = @"fullScreen"; NSDictionary *dictionary = [notification userInfo]; BOOL boolValue; if([dictionary valueForKey:key]) boolValue =[[dictionary valueForKey:key] boolValue]; } 
0
source

Source: https://habr.com/ru/post/1403092/


All Articles