key-value-observing

iOS: How do I know if a property is KVO-compliant?

偶尔善良 提交于 2019-11-27 04:31:28
In the Key-Value Observing Programming Guide , the section Registering for Key-Value Observing says "Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such." But, I haven't found any properties in the documentation that are documented as KVO-compliant. Would you please point me to some? Specifically, I would like to know if the @property(nonatomic,retain) UIViewController *rootViewController of UIWindow is KVO-compliant. The reason is that I'm adding the rootViewController property to UIWindow for iOS < 4 and want to know if I should make it KVO

NSNotification VS KVO

北慕城南 提交于 2019-11-27 03:38:15
问题 I feel that i don't fully understand difference between KVO and NSNotification... They seem to be so similar... Could you make some example showing when is best to use one method and when the other ? I don't speak about Bind and IB, but i mean add Observer programmatically in my code with NSNotificationCenter or KVO [self.preferenceController addObserver:self forKeyPath:@"color" options:NSKeyValueObservingOptionOld context:@"Color-change" ]; 回答1: KVO only works on values, NSNotification can

KVO and ARC how to removeObserver

天涯浪子 提交于 2019-11-27 03:08:29
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 addObserver:self forKeyPath:@"self.frame" options:NSKeyValueObservingOptionNew context:nil]; Previously, I would call removeObserver: in the view controller's dealloc method. You still can implement -dealloc under ARC, which appears to be the appropriate place to remove the observation of key values. You just don't call [super dealloc] from within this method any

KVO broken in iOS 9.3

旧城冷巷雨未停 提交于 2019-11-27 02:52:45
问题 This might be an awful bug in iOS 9.3 (release). When adding a single observer to [NSUserDefaults standardUserDefaults] I've noticed that the responding method -observeValueForKeyPath:ofObject:change:context: is called multiple times. In the simple example below, every time a UIButton is pressed once, observeValueForKeyPath fires twice. In more complicated examples it fires even more times. It is only present on iOS 9.3 (both on sim and devices). This can obviously wreak havoc on an app.

What is the context parameter used for in Key value observing

我与影子孤独终老i 提交于 2019-11-27 00:58:37
问题 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" options:NSKeyValueObservingOptionNew context:nil Can somebody shed some light what's the purpose behind it ... 回答1: I hope this explanation isn't too abstract to understand. Suppose you create a class MyViewController , which is a subclass of UIViewController . You don't have the

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

老子叫甜甜 提交于 2019-11-27 00:08:42
问题 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 observing that property? I've tried to ensure in my code that an object is only having an observer removed when it needs to be, but there are some cases where it's possible that the observer may try to remove itself twice. I'm working to prevent

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

匆匆过客 提交于 2019-11-26 23:06:22
问题 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 not have any remove -like method that receives an NSKeyValueObservation instance, the observer itself doesn't have any remove -like either. 回答1: In iOS 11, you don't have to. Just let the observer go out of scope. There is no penalty any longer for

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

跟風遠走 提交于 2019-11-26 22:30:16
问题 if you tell an objective c object to removeObservers: for a key path and that key path has not been registered, it cracks the sads. like - 'Cannot remove an observer for the key path "theKeyPath" from because it is not registered as an observer.' is there a way to determine if an object has a registered observer, so i can do this if (object has observer){ remove observer } else{ go on my merry way } 回答1: Put a try catch around your removeObserver call @try{ [someObject removeObserver

How reliable is KVO with UIKit

半城伤御伤魂 提交于 2019-11-26 21:34:20
问题 Important: Not all classes are KVO-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in “KVO Compliance.” Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such. This statement leaves me confused. Can't we use KVO for UIKit objects at all? I don't remember seeing any property being documented as KVO compliant. Despite saying otherwise, I am able to use KVO with many properties. Does

How to detect a property return type in Objective-C

不问归期 提交于 2019-11-26 20:13:17
问题 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? 回答1: You're talking about runtime property introspection, which happens to be something that Objective-C is very good at. In the case you describe, I'm assuming you have a class like this: @interface MyClass { NSArray * stuff; } @property (retain) NSArray * stuff; @end Which gets