Questions tagged [nsnotificationcenter]

NSNotificationCenter allows to send and register for notifications in the Foundation framework, which is provided by Apple.

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table. Notifications are differentiated according to their name and contain an object (the sender) as well as any specific data.

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

You can also post notifications with postNotification:, or create and post notifications with postNotificationName:object: and postNotificationName:object:userInfo:, where you may use custom names for your notifications.

References:

1577 questions
10
votes
1 answer

Status bar frame changes without any notification

I have registered to receive notifications about the status bar frame changes, but they are never received. Here is how I register for the notification: [[NSNotificationCenter defaultCenter] addObserver:self …
progrmr
  • 75,956
  • 16
  • 112
  • 147
9
votes
3 answers

Getting data from NSTask in real-time using notifications doesn't work

I got a Cocoa command-line program in which I try to run NSTask program (tshark to monitor network) and get data from it in real-time. So I make a NSFileHandle , call waitForDataInBackgroundAndNotify to send notifications and then register my…
PhoenixS
  • 1,016
  • 1
  • 9
  • 23
9
votes
2 answers

UIApplication.didBecomeActiveNotification Gets Called One-time Unexpectedly When User Swipe Down the Notification Center Screen on iOS Device

like the title says, I found this unexpected behavior in iOS 11 & 12. Does anyone know why this event gets called and, more importantly, is there any workaround to prevent system from firing this UIApplication.didBecomeActiveNotification event…
inexcii
  • 1,591
  • 2
  • 15
  • 28
9
votes
2 answers

NSNotificationCenter: Add observer but only if not registered to observe? Possible to query observing status for an object?

Is there a way to see if an object is already an observer for a type of notification? Currently, each addObserver call is paired with removeObserver to avoid duplicate observers, but is there a way to see if an object is already an observer before…
Crashalot
  • 33,605
  • 61
  • 269
  • 439
9
votes
2 answers

NSNotificationCenter: Removing an Observer in Swift

I have a view controller with a button. When the button is pressed it adds an observer, like so: func buttonPress(sender:UIButton){ NSNotificationCenter.defaultCenter().addObserverForName("buttonPressEvent", object:nil, queue:nil,…
Jimmery
  • 9,783
  • 25
  • 83
  • 157
9
votes
1 answer

kReachabilityChangedNotification is called multiple times

I'm using the Reachability classes for checking when I got an internet connection en when it goes down. This is my code: IN VIEW DID LOAD: internetReachable = [Reachability reachabilityForInternetConnection]; …
Steaphann
  • 2,797
  • 6
  • 50
  • 109
9
votes
2 answers

How to detect MPMoviePlayerController starts a movie?

I am using MPMoviePlayerController, how do I detect when the movie actually started playing - as opposed to when the user fiddles with the seek controls? From the tests I made, I always get a "load state change" event and (moviePlayer.loadState ==…
9
votes
2 answers

Detect Change in UILabel Text

Is it possible to set a Notification for when a UILabel's text property is changed? I tried the one used for UITextFields when I couldn't find one for a UILabel, but it didn't work. [[NSNotificationCenter defaultCenter] addObserver:self …
user717452
  • 33
  • 14
  • 73
  • 149
9
votes
5 answers

NSUserDefaultsDidChangeNotification: What's the name Of the Key, that Changed?

This code will call the method "defaultsChanged", when some value in UserDefaults changed NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(defaultsChanged:) …
smudo78
  • 478
  • 1
  • 3
  • 13
8
votes
1 answer

What is the difference between NSNotificationCenter and the Key Value Observing technique?

I just read a couple of tutorials regarding KVO, but I have not yet discovered the reason of its existence. Isn't NSNotificationCenter an easier way to observe objects? I am new to Stackoverflow, so just tell me if there is something wrong in the…
Erik
  • 2,138
  • 18
  • 18
8
votes
1 answer

Local notification sound not playing while app in background - Swift

I'm making an alarm clock app, and I have confirmed that my alarm notifications are being triggered correctly, but the sound does not always play as I expect. For instance, when the phone is not in silent mode, the notification plays the sound…
8
votes
3 answers

How to add an observer to NSNotificationCenter in a C++ class using Objective-C++?

I have a C++ class that I recently renamed from *.cpp to *.mm to support objective-c. So I can add the following objective-c code. [[NSNotificationCenter defaultCenter] addObserver:self …
valmo
  • 1,156
  • 2
  • 12
  • 22
8
votes
2 answers

Registering to receive notifications of remote CloudKit changes not working

I have just finished setting up CoreData+CloudKit using the new iOS 13 NSPersistentCloudKitContainer. It works really nicely in that I can use auto-generated CoreData classes for property access and local storage, and the…
mashers
  • 1,009
  • 7
  • 22
8
votes
2 answers

How write unit test for receiving nsnotification asynchronous?

I call rest web servic with completion handler and if succeed I send NSNotification. The problem is how to write unit test to assert that the notification is sent in case of success. Any help will be appreciated.
Abdelrahman
  • 997
  • 2
  • 10
  • 24
8
votes
2 answers

NSNotification subclass with Generics in Swift 2.1

Can't make subclass of NSNotification with Generic payload object. Getting either runtime error or compile error (see comments in code below). Is it even possible with Swift 2.1? Any ideas appreciated. Thanks! Runtime error because NSNotification is…
Vlad
  • 6,402
  • 1
  • 60
  • 74