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
13
votes
3 answers

Swift NotificationCenter remove observer quickest way

I am adding a number of observers in my viewController -- applicationWillResignActive, applicationDidEnterBackground, and many others. I want to remove self as observer to all registered notifications in one line. My question is whether the…
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
13
votes
4 answers

CoreData: error: Serious application error. Exception was caught during Core Data change processing

I am running into major issues developing my iphone app. here is the full error: CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of…
Ox Smith
  • 509
  • 1
  • 7
  • 20
13
votes
2 answers

How to retrieve all NSNotificationCenter observers?

I'd like to retrieve a list of observers (objects and selectors) for a given notification name. I know there's no official API for that. I also know I could subclass NSNotificationCenter to accomplish this. Sometimes however this is not a viable…
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
13
votes
5 answers

Mac Mountain Lion send notification from CLI app

How can I send a notification to the notification center from a command line app? My attemps so far compile and run, but don't succeed in notifying me. Example #import int main(int argc, const char * argv[]) { NSLog(@"Running…
13
votes
7 answers

When to use NSNotificationCenter

I want to have multiple observers on multiple events of a single object (1-to-N relationship). A mechanism to achieve this task is provided by the NSNotificationCenter. The mechanism looks pretty overkill when used for my problem. How I would do it…
Etan
  • 17,014
  • 17
  • 89
  • 148
12
votes
3 answers

When should UIViewController add/remove observers on NSNotificationCenter?

If I add an observer to the [NSNotificationCenter defaultCenter] in my viewDidLoad should I be removing it in viewDidUnload?
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
12
votes
1 answer

NSNotification removeObserver problem

I am either brain damaged or I am lacking of some understending of NSNotificationCenter The problem is that if I create an observer and in the next line will try to delete it like so: [[NSNotificationCenter defaultCenter] addObserver:self…
Cyprian
  • 9,423
  • 4
  • 39
  • 73
12
votes
5 answers

Swift iOS -AVPlayer Video Freezes / Pauses When App Comes Back from Background

I have a video playing in a loop on the login page of my app. I followed this Youtube tutorial to get it to work loop video in view controller The problem is when the app goes to the background, if I don't come back right away, when i do come back…
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
12
votes
4 answers

How to detect switch between macOS default & dark mode using Swift 3

I want to change my status bar app icon when the user switches from default to dark mode and vice versa (using Swift 3). Here’s what i have so far: func applicationDidFinishLaunching(_ aNotification: Notification) { …
12
votes
1 answer

Swift & NSUserNotification - No banner or alert, but is silently added to the notification list

I am learning Swift and wanted to display a simple user notification as a test. My build is successful, but no banner is shown, instead, the notification is silently added to the list of notifications. I have verified 'Do Not Disturb' is off, I…
Isaiah
  • 1,852
  • 4
  • 23
  • 48
12
votes
2 answers

NSNotification sent once, but is received multiple times

I am communicating between two classes with NSNotificationCenter. My problem is that although I tap a button once (and that button only fires off once) I am unintentionally producing increasing numbers of notifications from only one call to the…
Jimmery
  • 9,783
  • 25
  • 83
  • 157
12
votes
1 answer

where should i put removeObserver in AppDelegate.m, ios

My AppDelegate class is registered for a particular notification like below - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[NSNotificationCenter defaultCenter]…
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
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?
11
votes
1 answer

UIKeyboardWillShowNotification & UIKeyboardDidShowNotification report wrong keyboard height

The following code (sorry for the length) displays an odd behavior under iOS 4.3 (maybe others version too). In this example, there are three UITextFields that have three different sized keyboards. If you start editing one text field and then…
mmorris
  • 4,006
  • 3
  • 27
  • 29
11
votes
5 answers

NotificationCenter to pass data in Swift

I am working on a test project in Swift 3. I am trying to pass textField string from one class to another class using NotificationCenter. I am trying to workout the answer from this link: pass NSString variable to other class with NSNotification and…
Joe
  • 8,868
  • 8
  • 37
  • 59