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
11
votes
2 answers

Adding the same observer multiple times without removing it

What will happen if I added an observer multiple times without removing it? func registerForNotifications() { NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(foregroundNotification(_:)), name:…
iOS Developer
  • 187
  • 1
  • 2
  • 9
11
votes
4 answers

Swift 3 NSNotificationCenter Keyboardwillshow/hide

I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues. I have this code : let loginvc: LoginVC =…
RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
11
votes
4 answers

NSNotificationCenter removeObserver not working

-(void)viewDidAppear:(BOOL)animated { NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil…
S.J
  • 3,063
  • 3
  • 33
  • 66
11
votes
3 answers

Refreshing Parent ViewController after dismissing ModalViewController

In my iOS app, a user can select an image from a list, upon which they are presented with a modal that contains the image and options to delete the image. If the user chooses to delete the image, she is returned to the original viewController…
11
votes
2 answers

Xamarin NSNotificatioCenter: How can I get the NSObject being passed?

I am trying to post a notification in a view from my app to another one using NSNotificationCenter. So in my destination class I create my observer as follows: NSNotificationCenter.DefaultCenter.AddObserver ("ChangeLeftSide", delegate…
P. Sami
  • 2,115
  • 2
  • 21
  • 39
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
3 answers

postNotificationName not calling observer method

I am trying to call a method within an uiview from AppDelegate using the NSNotificationCenter to no avail.. AppDelegate.m [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:items]; Then via MainStoryboard,…
spacebiker
  • 3,777
  • 4
  • 30
  • 49
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
2 answers

Pass object with NSNotificationCenter to other view

I am trying to pass an object from my main view class to other notification receiver in another class. I want to pass an object named country, that loads all the cities from an SOAP Request in the Main Controller and i want to send it to my next…
Jorge Najera T
  • 1,511
  • 2
  • 20
  • 29
10
votes
2 answers

KVO versus NSNotifications

Is there any advantage to use KVO instead of the more "generic" (and to my opion more robust) feature of NSNotifications ?
Rabskatran
  • 2,269
  • 5
  • 27
  • 41
10
votes
2 answers

NSNotification being raised multiple times

i am developing a e-book reader and i had encountered the following issue. i am using an IBAction method to post a NSNotification which inturn invokes a action method once a button is tapped. It is working absolutely fine for the first time... The…
A for Alpha
  • 2,904
  • 8
  • 42
  • 76
10
votes
7 answers

iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h

Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the…
Mausimo
  • 8,018
  • 12
  • 52
  • 70
10
votes
1 answer

NotificationCenter Crash in Swift 3

Is it just me, or did NotificationCenter become a hot mess in Swift 3? :) I have the following setup: // Yonder.swift extension Notification.Name { static let preferenceNotification = Notification.Name("preferencesChanged") } // I fire the…
10
votes
3 answers

pausing spritekit game on app launch / exit .. iOS8

I've read everything I could find on this topic and still cant figure out my issue. I have tried pausing my game in every area of appdelegate func applicationWillResignActive(application: UIApplication!) { …
hamobi
  • 7,940
  • 4
  • 35
  • 64
10
votes
2 answers

The right place to call .removeObserver for NSNotificationCenter = Swift deinit()?

I've read a lot of suggestions for the right place to call .removeObserver for NSNotificationCenter since viewDidUnload is not an option. I was just wondering if the new deinit() in Swift would be a good choice? -nick
nick
  • 1,147
  • 1
  • 11
  • 12