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:

1561 questions
618
votes
6 answers

Send and receive messages through NSNotificationCenter in Objective-C?

I am attempting to send and receive messages through NSNotificationCenter in Objective-C. However, I haven't been able to find any examples on how to do this. How do you send and receive messages through NSNotificationCenter?
Cathy
419
votes
14 answers

NSNotificationCenter addObserver in Swift

How do you add an observer in Swift to the default notification center? I'm trying to port this line of code that sends a notification when the battery level changes. [[NSNotificationCenter defaultCenter] addObserver:self…
Berry Blue
  • 13,060
  • 16
  • 51
  • 91
314
votes
42 answers

Move view with keyboard using Swift

I have an app that has a text field on the lower half of the view. This means that when I go to type in the text field the keyboard covers the textfield. How would I go about moving the view upwards while typing so I can see what i'm typing and then…
Alex Catchpole
  • 6,546
  • 6
  • 18
  • 28
141
votes
6 answers

How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

I'm implementing socket.io in my swift ios app. Currently on several panels I'm listening to the server and wait for incoming messages. I'm doing so by calling the getChatMessage function in each panel: func getChatMessage(){ …
user3766930
  • 4,929
  • 9
  • 38
  • 91
131
votes
5 answers

How to pass object with NSNotificationCenter

I am trying to pass an object from my app delegate to a notification receiver in another class. I want to pass integer messageTotal. Right now I have: In Receiver: - (void) receiveTestNotification:(NSNotification *) notification { if…
Jon
  • 4,652
  • 6
  • 40
  • 67
121
votes
13 answers

How do you create custom notifications in Swift 3?

In Objective-C, a custom notification is just a plain NSString, but it's not obvious in the WWDC version of Swift 3 just what it should be.
97
votes
8 answers

Android equivalent to NSNotificationCenter

In the process of porting an iPhone application over to android, I am looking for the best way to communicate within the app. Intents seem to be the way to go, is this the best (only) option? NSUserDefaults seems much lighter weight than Intents do…
John
  • 1,429
  • 2
  • 13
  • 24
85
votes
7 answers

iOS 11 - Keyboard Height is returning 0 in keyboard notification

I have been using Keyboard notifications without any problem and getting exact height of Keyboard. - (void)keyboardDidShow:(NSNotification *) notification{ CGSize keyboardSize = [[[notification userInfo]…
Hassy
  • 3,622
  • 3
  • 29
  • 54
72
votes
2 answers

how to use the object property of NSNotificationcenter

Could somebody please show me how to use the object property on NSNotifcationCenter. I want to be able to use it to pass an integer value to my selector method. This is how I have set up the notification listener in my UI View. Seeing as I want an…
dubbeat
  • 7,275
  • 18
  • 66
  • 115
70
votes
4 answers

Posting NSNotification on the main thread

I found the following code snippet which allows NSNotification to be posted on the main thread from any background thread. I would like to know if this is a safe and acceptable practice please? dispatch_async(dispatch_get_main_queue(),^{ …
RunLoop
  • 19,113
  • 21
  • 89
  • 146
67
votes
3 answers

Post of NSNotificationCenter causing "EXC_BAD_ACCESS" exception

A UIViewController adds itself to the default center: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFood) name:@"editFood" object:nil]; Then a UITableView delegate NSObject posts a…
60
votes
4 answers

OS X: Detect system-wide keyDown events?

I'm working on a typing-tutor application for Mac OS X that needs to have keystrokes forwarded to it, even when the application is not in focus. Is there a way to have the system forward keystrokes to the app, possibly through…
Chris Ladd
  • 2,735
  • 1
  • 26
  • 22
51
votes
9 answers

How to receive NSNotifications from UIWebView embedded YouTube video playback

I didn't received any notifications for MPMoviePlayerController. What am I doing wrong? I use following logic. I'm begining to play youtube video in UIWebView. UIWebView calls a standard MPMoviePlayerController. I don't control…
48
votes
1 answer

How can I listen for all notifications sent to the iOS NSNotificationCenter's defaultCenter?

I want to listen to all notifications dispatched to the defaultCenter. Both public and private. Does anyone know how I can do this?
Tony
  • 17,359
  • 29
  • 118
  • 188
39
votes
3 answers

How to pass a NSDictionary with postNotificationName:object:

I am trying to pass an NSDictionary form a UIView to a UIViewController using NSNotificationCenter. The dictionary works fine at the time the notification is posted, but in the receiving method I am unable to access any of the objects in the…
user278859
  • 8,979
  • 12
  • 48
  • 73
1
2 3
99 100