Questions tagged [key-value-observing]

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

1201 questions
32
votes
5 answers

How to use KVO for UserDefaults in Swift?

I'm rewriting parts of an app, and found this code: fileprivate let defaults = UserDefaults.standard func storeValue(_ value: AnyObject, forKey key:String) { defaults.set(value, forKey: key) defaults.synchronize() …
Sti
  • 8,275
  • 9
  • 62
  • 124
32
votes
5 answers

An -observeValueForKeyPath:ofObject:change:context: message was received but not handled

I am relatively new to KVO, so there is a good chance that I am violating some fundamental rule. I am using Core Data. My app crashes with the following message: And what I can't understand is why a CGImage is getting involved in observing a value…
westsider
  • 4,967
  • 5
  • 36
  • 51
28
votes
5 answers

Adding observer for KVO without pointers using Swift

In Objective-C, I would normally use something like this: static NSString *kViewTransformChanged = @"view transform changed"; // or static const void *kViewTransformChanged = &kViewTransformChanged; [clearContentView addObserver:self …
Justin Moore
  • 884
  • 2
  • 9
  • 22
28
votes
1 answer

What is the context parameter used for in Key value observing

What's the use of context parameter in following method which is used to register for key value notifications. The documentations just denotes it as arbitrary set of data. addObserver:self forKeyPath:@"selectedIndex"…
rustylepord
  • 5,681
  • 6
  • 36
  • 49
27
votes
5 answers

How do I find all the property keys of a KVC compliant Objective-C object?

Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol? Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant…
armahg
  • 739
  • 2
  • 10
  • 11
25
votes
4 answers

Observing changes to a UIView's window and superview properties

I'm looking for a way to be notified when a generic UIView is added or removed from the visible view hierarchy. KVO looked like the perfect thing to use in this case, but observing changes to a view's window or superview properties doesn't do…
Mattia
  • 2,251
  • 1
  • 22
  • 27
25
votes
4 answers

How listen for UIButton state change?

I'm extending UIButton with generic functionality to change certain appearance attributes based on the displayed title. In order to do this, I need to detect and respond to changes in the "state" property. This is so I make sure the appearance is…
DougW
  • 28,776
  • 18
  • 79
  • 107
25
votes
4 answers

"message was received but not handled" KVO

I'm getting the following in my output window when running a project: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. Key path: connection.messageQueue Observed object: ... You get the…
Kieran Senior
  • 17,960
  • 26
  • 94
  • 138
22
votes
2 answers

Checking if a value is changed using KVO in Swift 3

I would like to know when a set of properties of a Swift object changes. Previously, I had implemented this in Objective-C, but I'm having some difficulty converting it to Swift. My previous Objective-C code is: - (void)…
aepryus
  • 4,715
  • 5
  • 28
  • 41
21
votes
2 answers

NSManagedObject and KVO vs Documentation

I have a custom NSManagedObject subclass, say, Person. I also have a UIView registered with -addObserver:forKeyPath:options:context: to observe various properties of a Person, some of which are persistent like "name" and others are just dumb…
Costique
  • 23,712
  • 4
  • 76
  • 79
21
votes
1 answer

Key Value Observing with an NSArray

I've looked on SO for examples of using Key Value Observing with an NSArray (or NSMutableArray) and apparently you need to use an NSArrayController (which unlike KVO I'm not familiar with), but I haven't found concrete examples of how to do this.…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
21
votes
4 answers

How to be notified when a UIView detached from its superView?

It seems that the UIView has not methods like "didRemoveFromSuperview" or "willRemoveFromSuperview".Then,How to listen to the event when a UIView removed from its superView?I should use KVO? thanks in advance!
Jagie
  • 2,190
  • 3
  • 27
  • 25
20
votes
5 answers

Is it possible to observe a readonly property of an object in Cocoa Touch?

I've attempted to observe the (readonly) visibileViewController property of a UINavigationController with no success. I was able to successfully observe a readwrite property I defined myself for testing purposes on another class. Is it possible to…
user53937
  • 623
  • 1
  • 9
  • 10
20
votes
4 answers

Why does NSOperation disable automatic key-value observing?

When working with a custom NSOperation subclass I noticed that the automatic key-value observing is disabled by the [NSOperation automaticallyNotifiesObserversForKey] class method (which returns NO at least for some key paths). Because of that the…
zoul
  • 102,279
  • 44
  • 260
  • 354
19
votes
6 answers

How to detect a property return type in Objective-C

I have an object in objective-c at runtime, from which I only know the KVC key and I need to detect the return value type (e.g. I need to know if its an NSArray or NSMutableArray) of this property, how can I do that?
Enyra
  • 17,542
  • 12
  • 35
  • 44
1
2
3
80 81