Questions tagged [nsnotification]

NSNotification is a class used to send messages to observing classes through NSNotificationCenter.

NSNotification is a class used to send messages to observing classes through NSNotificationCenter. Notifications must have a name and can be optionally associated with a specific object. If an object is provided, the notification is only delivered to a class that is also observing the exact same name and object.

To send a notification, use NSNotificationCenter's postNotification: (a convenience function postNotificationName:object:userInfo: also exists which creates a notification and posts it with a single call). Classes posting a notification can also include an optional userInfo parameter to pass an object to the receiving class.

For more information, see Apple's documentation for NSNotification and NSNotificationCenter.

312 questions
5
votes
2 answers

NSNotification to detect other program's crashes

I was planning on writing a small daemon that detected whether another app crashed, thinking all the while that the system would send an NSWorkspaceDidTerminateApplicationNotification, but this is not the case. Assuming that I do not want to create…
Andrew J. Freyer
  • 591
  • 2
  • 12
  • 38
5
votes
1 answer

addobserver / removeobserver query

Is it OK to use -removeObserver: first and then call -addObserver: with the same name? Or is it a rule to have -addObserver: first before -removeObserver:? I tried it using OS 4.0 and it seems OK (no crash, warnings... etc.). -(void)…
mark
  • 51
  • 1
  • 2
5
votes
1 answer

How to create a class to send and receive events through NSNotificationCenter in Objective-C?

I need to create two classes and both should be able to send and receive the events through the NSNotificationCenter methods.ie Both should have the sendEvent and receiveEvent methods: @implementation Class A -(void)sendEvent { …
Cathy
  • 797
  • 2
  • 14
  • 25
5
votes
3 answers

Is there a way to check if observer listens to some NSNotification?

I want to check if my view is listening for UIApplicationWillResignActiveNotification or not. If it is listening then I want to remove it during dealloc. Now I was wondering if there is way to do this using objective c ? I am not trying avoid…
5
votes
2 answers

Logging NSNotifications

I would like to log any NSNotifications posted by a single NSNotificationCenter shared accross my application. I have tried subclassing NSNotificationCenter with the intention of adding logging code to the three post methods, but it returns an…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
4
votes
1 answer

Should I unregister NSNotification both in viewDidUnload and dealloc?

I register a NSNotification in viewDidLoad method. Should I unregister it both in viewDidUnload and dealloc method using the code below? [[NSNotificationCenter defaultCenter] removeObserver:self]; Thanks.
tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
4
votes
1 answer

How to migrate NSWorkspace notifications to Swift 4?

In Swift 3 I registered for sleep and wake notifications with this code: let notificationCenter = NSWorkspace.shared.notificationCenter notificationCenter.addObserver(self, selector: #selector(AppDelegate.sleepListener), name:…
Daniel Compton
  • 13,878
  • 4
  • 40
  • 60
4
votes
0 answers

Value for UIKeyboardAnimationDurationUserInfoKey is zero when QuickType bar shows/hides. Why?

I use the UIKeyboardWillChangeFrameNotification for changing my views when the keyboard is shown. To animate along with the keyboard, I use the NSNotification's user info dictionary to get the duration of the animation, with the…
ArVID220u
  • 374
  • 3
  • 10
4
votes
2 answers

AVPlayerItemDidPlayToEndTimeNotification not being called with iOS 8

AVPlayerItem *currentItem = self.player.currentItem; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:currentItem]; I have the above notification…
rcat24
  • 548
  • 2
  • 23
4
votes
2 answers

Forcing a language change notification iOS

my app is being localized and we are concerned with dialects. (ie. difference between spanish and spanish in peurto rico) I notice that just changing the region doesn't push a notification to the app that will trigger the localization.. only…
erik
  • 4,946
  • 13
  • 70
  • 120
4
votes
1 answer

Sending data between VCs using NSNotificationCenter

I need to pass a NSMutableDictionary from one class (ViewControllerA) to another (ViewControllerB) using NSNotificationCenter. I have tried the following code but it doesn't work. I actually pass to the ViewControllerB but the -receiveData method…
4
votes
1 answer

NSNotification troubles

When my class initializes, it adds itself as an observer for a bunch of different Wi-Fi notifications. For some reason, the selector isn't running when any of these things happen. Any ideas? Thank you ahead of time. -(id) init { if (self) { …
Tanner Silva
  • 279
  • 3
  • 14
3
votes
3 answers

Creating No Empty Selections in NSCollectionView

I have set up an NSCollectionView in a cocoa application. I have subclassed the collection view's NSCollectionViewItem to send me a custom NSNotification when one of its views it selected / deselected. I register to receive a notification within my…
GravityScore
  • 385
  • 1
  • 3
  • 13
3
votes
2 answers

Giving notification to another class with NSNotificationCenter

So my goal is to deliver a notification to another class with using NSNotificationCenter, I also want to pass object with the notification to the other class, how should I do this?
Samuli Lehtonen
  • 3,840
  • 5
  • 39
  • 49
3
votes
1 answer

Obj-C Cocoa Notification NSApplicationDidResignActiveNotification

I've a class called AppController.h/m I want to make something when the NSNotificationDidResignActiveNotification is sent. So i wrote this code in AppController.m: -(void) initialize(){ [[NSNotificationCenter defaultCenter] addObserver:self …
tmnd91
  • 449
  • 1
  • 6
  • 23
1 2
3
20 21