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
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.
28
votes
2 answers

NSNotifications name best practice

In trying to de-couple my model from the view controllers that display fetched data, when an asynchronous fetch completes, I post an NSNotification. [[NSNotificationCenter defaultCenter] postNotificationName:@"foobarFetchSuccess" object:…
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
28
votes
3 answers

removeObserver with NSNotification... what am I doing wrong?

Basically, I have a view1 which at some point, calls view2 (via presentModalViewController:animated:). When a certain UIButton in view2 is pressed, view2 is calls a notification method in view1 and immediately afterward is dismissed. The…
Derek
  • 311
  • 1
  • 4
  • 5
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
2 answers

Does the NSNotification retain the object?

My question is in regards the object that gets added to a -postNotificationName:object: userInfo: method. Does the NSNotification retain the object ? (in a similar fashion to NSMutableDictionary or Array) ... meaning I can release the object…
MDMonty
  • 363
  • 2
  • 10
18
votes
1 answer

NSNotificationCenter passing structs as part of the UserInfo

Due to NSNotificationCenter.defaultCenter().postNotificationName userinfo only accepting dictionaries with data complying with AnyObject protocol, does anyone have any suggestions how to post structs as part of an NSNotification? My initial thought…
Daniel Creagh
  • 502
  • 4
  • 11
17
votes
5 answers

UIKeyboardDidShowNotification called multiple times, and sometimes with incorrect keyboard dimensions

I am trying to move a UITextView above the keyboard whenever the keyboard appears/changes. Let's say I have the English keyboard displaying and then switch directly to the Chinese keyboard (which is taller than the standard English keyboard). In…
Benny B
  • 357
  • 1
  • 3
  • 13
16
votes
4 answers

Which is a better way to remove Notification observer

I usually use NSNotification like the sample below: In viewDidLoad: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(foo:) name:kName1 object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self…
tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
13
votes
1 answer

Using only UIKeyboardWillChangeFrameNotification notification

Considering the new QuickType section of the keyboard. Is it quite true that one can use ONLY a notification for UIKeyboardWillChangeFrameNotification, and simply "not bother with" the "older" UIKeyboardWillShowNotification and…
Fattie
  • 27,874
  • 70
  • 431
  • 719
13
votes
3 answers

What is NSNotification?

Can anybody explain the importance of NSNotificationCenter? Where to use them? What is the difference between NSNotificationCenter vs. AppDelegate?
raaz
  • 12,410
  • 22
  • 64
  • 81
11
votes
2 answers

KVO observation vs NSNotificationCenter observation

I'm wondering if there is a reason to use one over the other in KVO vs NSNotificationCenter observations. Performance, memory usage, speed, etc?
10
votes
1 answer

Text from UITextFieldTextDidChangeNotification

I have a UITextField with this NSNotification: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:_searchTextField]; - (void)textFieldDidChange…
daihovey
  • 3,485
  • 13
  • 66
  • 110
10
votes
2 answers

How to determine which NSNotification is crashing due to dealloc-ed observer

The golden rule of using NSNotification seems to be "call removeObserver before the observer (or the object) is deallocated". I'm dealing with a codebase where this rule hasn't been followed, but I can't locate the transgression. I've searched…
10
votes
4 answers

Is subclassing NSNotification the right route if I want to add typed properties?

I am trying to subclass NSNotification. Apple's docs for NSNotificationstate the following: NSNotification is a class cluster with no instance variables. As such, you must subclass NSNotification and override the primitive methods name, object,…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
10
votes
1 answer

MPMoviePlayerController will not automatically dismiss movie after finish playing (ios 6)

I may not have worded my title very well, maybe more correct to say my NSNotification isn't dismissing my movie's view after it's done playing. I've found others with this problem but no solutions, seems like it might be a problem with iOS 6 which…
1
2 3
20 21