Questions tagged [addobserver]

61 questions
18
votes
5 answers

Adding and removing observers to NSNotificationCenter in a UIViewController

Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad, then remove them in dealloc. This seems dangerous as viewDidLoad can be called multiple times without…
Undistraction
  • 38,727
  • 46
  • 165
  • 296
14
votes
3 answers

Swift 4 - Notification Center addObserver issue

I'm crashing and getting an unrecognized selector error every time a Notification arrives and the App tries to execute its associated method. Here's my code - which is in viewDidLoad: let notificationCenter =…
Sirab33
  • 971
  • 3
  • 9
  • 15
10
votes
4 answers

Add Observer to BOOL variable

Is it possible to add observers to simple variables such as BOOLs or NSIntegers and see when they change? Thanks!
Andrei
  • 7,340
  • 19
  • 71
  • 116
8
votes
3 answers

Possible locations to call addObserver and removeObserver methods

I have a case where the child view sends notification to its parent view. Now I'm calling addObserver: in viewWillAppear: and removeObserver: in viewWillDisappear:. But, I'm guessing this is not correct since viewWillAppear: calls when view is…
Mustafa
  • 20,108
  • 39
  • 139
  • 208
5
votes
2 answers

Swift 2 addObserver for specific textField with the object parameter

To my understanding the object parameter of the addObserver method is the object you want to receive notifications from. Most of the time I see it as nil (I assume this is because notifications of the specified type are wanted from all objects). In…
sbru
  • 837
  • 4
  • 19
  • 36
4
votes
1 answer

addObserver on self

A pretty simple question, but I can't seem to find an answer. (Developing an iOS 5+ app). In my AppDelegate, I have a property, let's call it @property (non atomic) BOOL aFlag;. I'd like my AppDelegate to be notified if the value changes. Here is…
rdurand
  • 7,119
  • 3
  • 36
  • 70
3
votes
2 answers

viewWillAppear and UIApplicationDidBecomeActiveNotification

I am using the UIApplicationDidBecomeActiveNotification to refresh my tableview when the app becomes active. My problem is that in my ViewWillAppear, I am also calling a method to refresh this table's data. This is causing the table to be refreshed…
edhnb
  • 1,850
  • 2
  • 22
  • 34
3
votes
1 answer

observeValueForKeyPath not calling

I am using observer following code myView = [[UIView alloc] initWithFrame:CGRectZero]; [myView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; Another Code - (void)observeValueForKeyPath:(NSString…
Amit Battan
  • 2,848
  • 2
  • 30
  • 68
3
votes
2 answers

How to add observers to an NSManagedObject?

What is the preferred approach to registering observers on an NSManagedObject (since Core Data "'owns' the lifecycle" of managed objects)? Am I correct that the way to do this is to listen for NSManagedObjectContextObjectsDidChange-Notification and…
orome
  • 35,904
  • 38
  • 156
  • 345
3
votes
2 answers

addObserver/removeObserver in viewWillAppear/viewWillDisappear

I call addObserver in viewWillAppear and removeObserver in viewWillDisappear. It seems to work fine. However, in the Xcode crash logs I'm receiving reports that some customers crash when I call removeObserver (21 crashes yesterday...). I haven't had…
Steve A
  • 1,220
  • 3
  • 14
  • 26
3
votes
1 answer

NSNotification - check if an observer is added before posting a notifiation

my code is crashing at: [[NSNotificationCenter defaultCenter] postNotificationName:kgotNotification object:dictionary]; my assumption is that I am posting a notification before the observer is added. [[NSNotificationCenter defaultCenter]…
shebelaw
  • 3,562
  • 6
  • 31
  • 48
3
votes
1 answer

How to observe a key path like "object.attribute"

greetings , I'm a cocoa beginner and this is my first post:P I'm trying to make a very simple rhythm game but get stuck , here's what I got: /**** TouchView.h/m ****/ @property AVAudioPlayer *audioPlayer; [self addObserver:self …
mario.hsu
  • 97
  • 5
1
vote
1 answer

Why is addObserver crashing instantly?

I have various kinds of operations (derived from NSOperation) to do async queries over the Internet. As is the norm, I determine when they're finished by observing their isFinished property. When one particular type of operation finishes, I want to…
Oscar
  • 1,603
  • 1
  • 24
  • 36
1
vote
1 answer

NSNotificationCenter addObserver not responding

In my app (game) I'm trying to use the NSNotificationCenter to pause and resume the game when either the center/home or lock button is pressed. This is the code I'm using: [[NSNotificationCenter defaultCenter] addObserver:self …
eric.mitchell
  • 8,501
  • 12
  • 50
  • 91
1
vote
1 answer

How to pass parameter to selector in swift?

func addObserver(_ scrollView: UIScrollView) { NotificationCenter.default.addObserver(self, selector: #selector(XXX), name:, object:) } @objc func getScrollView (_ notification: Notification, useScrollView : UIScrollView) { // Doing something…
LightOwlGG
  • 109
  • 6
1
2 3 4 5