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
25
votes
9 answers

OSX Notification Center Icon

I'm using OSX's Notification Center APIs for the first time and can't seem to figure out how to make my app's icon to show up in the Notification badge. The default "your app doesn't have an icon" icon keeps showing up: Here's what I've done so far…
phatduckk
  • 1,775
  • 1
  • 14
  • 17
23
votes
3 answers

How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to see a quick example.
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
23
votes
7 answers

thumbnailImageAtTime: now deprecated - What's the alternative?

Until iOS7 update I was using... UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; ...with great success, so that my app could show a still of the video that the user had just taken. I understand…
Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
23
votes
3 answers

When to create a custom NSNotificationCenter?

I've been playing about with the NSNotificationCenter and I've been wondering when you would use your own custom notification center rather than the defaultCenter? And what would the advantages of this be? Forgive my ignorance, but it seems like I…
Jimmery
  • 9,783
  • 25
  • 83
  • 157
23
votes
3 answers

NSNotification order of observer notifications

If I have several classes observing a particular NSNotification, in what order are the observers notified when the notification is posted?
whatdoesitallmean
  • 1,586
  • 3
  • 18
  • 40
22
votes
3 answers

Warning for iOS/iPhone users about duplicate NSNotification observations

This isn't a question so much as a warning to others to save them some time. NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior: If you register yourself multiple times for the exact specific…
God of Biscuits
  • 1,312
  • 1
  • 13
  • 27
22
votes
8 answers

How do I check if an UIViewController is currently being displayed?

How do I check if an UIViewController is currently being displayed? My UIViewControllers are listening for NSNotifications - even if when they are not displayed (ie not shown). So I could have 10 UIViewController in the background observing…
21
votes
3 answers

Removing a NSNotificationCenter observer in iOS 5 ARC

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done…
Skoota
  • 5,280
  • 9
  • 52
  • 75
20
votes
3 answers

Type 'NSNotification.Name' has no member 'UITextField'

With Swift 4.2, getting following error, that was working fine with Swift 4. Type 'NSNotification.Name' has no member 'UITextField' Here is my error code. NotificationCenter.default.addObserver(forName:…
Krunal
  • 77,632
  • 48
  • 245
  • 261
20
votes
10 answers

NSNotificationCenter Swift 3.0 on keyboard show and hide

I am trying to run a function when the keyboard shows and disappears and have the following code: let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification…
Dhwanit Zaveri
  • 465
  • 1
  • 5
  • 15
20
votes
3 answers

NSUserNotificationCenter dismiss notification

I'm trying to use the new Mountain Lion NSUserNotificationCenter for my application (which isn't too hard actually). Posting notifications works like a charm via NSUserNotification *userNotification = [[NSUserNotification alloc]…
19
votes
4 answers

NSOperation and NSNotificationCenter on the main thread

I have an NSOperation. When it is finished I fire a NSNotificationCenter to let the program know that the NSoperation is finished and to update the gui. To my understanding listeners to the NSNotification will not run on the main thread because the…
Mel
  • 2,055
  • 2
  • 26
  • 45
19
votes
4 answers

Detect rotation changes in iOS

I am making an iOS app that needs to do a little interface rearrangement upon rotation. I am trying to detect this by implementing - (void)orientationChanged:(NSNotification *)note, but this gives me notifications for when the device is face up or…
Undo
  • 25,519
  • 37
  • 106
  • 129
19
votes
2 answers

Observing Changes to a mutable array using KVO vs. NSNotificationCenter

In my model I have an array of objects called events. I would like my controller to be notified whenever a new object is added to events. I thought that a good way to do this would be use the KVO pattern to get notified when the events changes (from…
19
votes
5 answers

Adding and removing observers to NSNotificationCenter in a UIViewController

Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad, then remove them in dealloc. This seems dangerous as viewDidLoad can be called multiple times without…
Undistraction
  • 42,754
  • 56
  • 195
  • 331