Questions tagged [key-value-observing]

Key-value Observing or KVO is a technology for observing changes in object properties.

1201 questions
178
votes
11 answers

Is key-value observation (KVO) available in Swift?

If so, are there any key differences that weren't otherwise present when using key-value observation in Objective-C?
codeperson
  • 8,050
  • 5
  • 32
  • 51
165
votes
4 answers

What's the best way to communicate between view controllers?

Being new to objective-c, cocoa, and iPhone dev in general, I have a strong desire to get the most out of the language and the frameworks. One of the resources I'm using is Stanford's CS193P class notes that they have left on the web. It includes…
A Hopeful Soul
155
votes
7 answers

Why would you use an ivar?

I usually see this question asked the other way, such as Must every ivar be a property? (and I like bbum's answer to this Q). I use properties almost exclusively in my code. Every so often, however, I work with a contractor who has been developing…
Sam
  • 26,946
  • 12
  • 75
  • 101
143
votes
11 answers

How can I tell if an object has a key value observer attached

If you tell an Objective-C object to removeObservers: for a key path and that key path has not been registered, it cracks a sad, like: Cannot remove an observer for the key path "theKeyPath" from because it is…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
87
votes
3 answers

KVO and ARC how to removeObserver

How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing? For example, on a view controller: [self.view…
drunknbass
  • 1,662
  • 1
  • 13
  • 19
82
votes
9 answers

How can I do Key Value Observing and get a KVO callback on a UIView's frame?

I want to watch for changes in a UIView's frame, bounds or center property. How can I use Key-Value Observing to achieve this?
hfossli
  • 22,616
  • 10
  • 116
  • 130
76
votes
7 answers

Observing an NSMutableArray for insertion/removal

A class has a property (and instance var) of type NSMutableArray with synthesized accessors (via @property). If you observe this array using: [myObj addObserver:self forKeyPath:@"theArray" options:0 context:NULL]; And then insert an object in the…
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
47
votes
2 answers

Swift 4 approach for observeValue(forKeyPath:...)

I've been trying to find an example, but what I've seen doesn't work in my case. What would be the equivalent of the following code: object.addObserver(self, forKeyPath: "keyPath", options: [.new], context: nil) override public func observeValue( …
MVZ
  • 2,220
  • 5
  • 27
  • 57
47
votes
4 answers

Observing change in UIDatePicker

I noticed that there is no delegate to observe changes in UIDatePicker. Is there a way to detect when a change is made in the picker without confirming anything, like the moment it spins and lands on a new number I want to be able to detect that. I…
Chris
  • 7,270
  • 19
  • 66
  • 110
45
votes
3 answers

Best practices for context parameter in addObserver (KVO)

I was wondering what you should set the Context pointer in KVO when you are observing a property. I'm just starting to use KVO and I haven't gleaned too much from the documentation. I see on this page:…
Crystal
  • 28,460
  • 62
  • 219
  • 393
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
42
votes
2 answers

In Swift 4, how do I remove a block-based KVO observer?

If I store an observer like this: let observer: NSKeyValueObservation = foo.observe(\.value, options: [.new]) { (foo, change) in print(change.newValue) } How do I remove/disable/cleanup observer once I no longer need it? My foo instance does…
Guilherme
  • 7,839
  • 9
  • 56
  • 99
41
votes
3 answers

KVO - How to check if an object is an observer?

When observing a value on an object using addObserver:forKeyPath:options:context:, eventually you'll want to call removeObserver:forKeyPath: on that object to clean up later. Before doing that though, is it possible to check if an object actually is…
Josh Buhler
  • 26,878
  • 9
  • 29
  • 45
37
votes
3 answers

Simple KVO example

I am trying to do simple KVO example, but I am having problems. This is my *.m file: #import "KVO_ViewController.h" @interface KVO_ViewController () @property NSUInteger number; @end @implementation KVO_ViewController - (void)viewDidLoad { …
WebOrCode
  • 6,852
  • 9
  • 43
  • 70
32
votes
4 answers

When should I remove observers? Error about deallocating objects before removing observers

I am trying to use key-value observing in one of my classes. I register the observers in the init method and remove/deregister them in the dealloc, but I get the following error which seems to occur before my dealloc method gets called, according to…
1
2 3
80 81