Questions tagged [nsnotifications]

NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object.

NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object.

An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification.

The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any.

NSNotification objects are immutable objects.

For more information, read Apple's NSNotification class reference.

474 questions
167
votes
7 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
  • 5,629
  • 10
  • 51
  • 104
137
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,732
  • 6
  • 44
  • 67
133
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.
104
votes
14 answers

Objective-C: Where to remove observer for NSNotification?

I have an objective C class. In it, I created a init method and set up a NSNotification in it //Set up NSNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData) …
Zhen
  • 12,361
  • 38
  • 122
  • 199
100
votes
11 answers

Where to remove observer for NSNotification in Swift?

Where should I remove the observer for NSNotification in Swift, since viewDidUnload and dealloc() are unavailable?
Clement Joseph
  • 1,235
  • 2
  • 13
  • 17
70
votes
6 answers

NSNotificationCenter vs delegation( using protocols )?

What are the pros and cons of each of them? Where should I use them specifically?
EEE
  • 4,536
  • 3
  • 28
  • 34
64
votes
9 answers

How to stop the Observer in NSNotification to called twice?

I have an observer of NSNotification which is called twice. I do not know what to do with it. I googled it but no solution found. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectedToServer:)…
Azhar
  • 20,500
  • 38
  • 146
  • 211
56
votes
5 answers

Text change notification for an NSTextField

I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored in the NSTextField. [[NSNotificationCenter defaultCenter] …
alecail
  • 3,993
  • 4
  • 33
  • 52
44
votes
5 answers

How to avoid adding multiple NSNotification observer?

Right now the API doesn't seem to provide a way to detect if an observer has already been added for a particular NSNotification. What's the best way to avoid adding multiple NSNotification observers other than maintaining a flag on your end to keep…
Boon
  • 40,656
  • 60
  • 209
  • 315
42
votes
6 answers

KVO vs NSNotification vs protocol/delegates?

I have some idea of which to use when but the exact usage is still not clear to me. Can someone explain with example?
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
38
votes
3 answers

How to use Notification.Name extension from Swift to Objective-C?

I created an extension for Notification.Name as below: public extension Notification.Name { public static let blahblahblah = Notification.Name(rawValue: "blahblahblah") } Now I want to use this extension in Objective-C, but it's not accessible…
BARS
  • 629
  • 1
  • 6
  • 18
35
votes
5 answers

NSNotificationCenter trapping and tracing all NSNotifications

For some better understanding on what happens “under the hood”, I would love to do a complete trace of any notifications happening within my application. Naïve as I am, the first thing I tried was registering like this: Somewhere in my app: { …
Till
  • 27,559
  • 13
  • 88
  • 122
30
votes
5 answers

Why is my NSNotification its observer called multiple times?

Within an App I make use of several viewcontrollers. On one viewcontroller an observer is initialized as follows: [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil]; [[NSNotificationCenter defaultCenter]…
BarryK88
  • 1,806
  • 2
  • 25
  • 41
24
votes
1 answer

iOS "Gboard" app, UIKeyboard notification height is wrong or invalid

I'm trying to move the view up when the keyboard pops up over the UITextfield which is placed on UIScrollView. I'm using UIKeyboardWillShowNotification and UIKeyboardWillHideNotification for this. It's working perfectly when using iOS Keyboard…
arthankamal
  • 6,341
  • 4
  • 36
  • 51
23
votes
8 answers

access object passed in NSNotification?

I have a NSNotification that is posting a NSDictionary: NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: anItemID, @"ItemID", [NSString…
Slee
  • 27,498
  • 52
  • 145
  • 243
1
2 3
31 32