Some help observing key values ​​with Slider

I monitor key values, but this is something that I don't quite understand.

I have a slider (sldMoving) which, when moving, pretends that the console displays "changed." Although there are other ways to do this, I'm good at studying.

In the view - (void) viewDidLoad implements it well:

[super viewDidLoad]; [self.sldMoving addObserver:self forKeyPath:@"self.sldMoving.value" options:NSKeyValueObservingOptionNew context:NULL]; 

and in the observationValueKeyPath:

 -(Void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context { if ([keyPath isEqualToString:@"self.sldMoving.value"]) { NSLog(@"I Have change"); 

Obviously, this nonsense that I wrote does not work.

Error:

2012-08-25 20: 17: 07.611 Example [3947: c07] * Application termination to exclude the exception "NSUnknownKeyException", reason: "[addObserver: forKeyPath: @" sldMoving.value "options: 0x101 context: 0x0] object was sent which does not match KVC property "sldMoving" * Origin call stack cast:. (0x136b022 0x1060cd6 0x1313a48 0x13139b9 0xadc84f 0xadeb0c 0xaddd3f 0xadc6fd 0x27ff 0x28aa1e 0x1e9401 0x1e9670 0x1e9836 0x1f072a 0x1c1596 0x1c2274 0x1d1183 0x1d1c38 0x1c5634 0x2177ef5 0x133f195 0x12a3ff2 0x12a28da 0x12a1d84 0x12a1c9b 0x1c1c65 0x1c3626 0x1efd 0x1e65) termination throwing exception

How can i solve this?

Thanks.

(excuse me, my bad english)

+4
source share
2 answers

I understand that I want to learn, but you should probably do it on something different from the controller. In general, UIKit does not support KVO unless explicitly documented.

If you want to control the control, you need to use the standard control API.

However, to help you understand what is happening with your error ...

Are you trying to use this code ...

 [self.sldMoving addObserver:self forKeyPath:@"self.sldMoving.value" options:NSKeyValueObservingOptionNew context:NULL]; 

which translates freely into something like that ...

"Yo, sldMoving , I want you to add an observer object ( self ). Now that the observer wants to see what happens with this path: self.sldMoving.value , namely the new changes.

So, the sldMoving object looks inside of itself to see if this key path makes sense. However, this does not make sense, so it raises this exception. Why doesn't that make sense? Well, there is no way to go to the way, because these names do not make sense to UISlider.

Now, if you changed it like this:

 [self addObserver:self forKeyPath:@"sldMoving.value" options:NSKeyValueObservingOptionNew context:NULL]; 

You will at least get a successful registration. It talks to the self object and wants it to observe sldMoving.value , which it can do because it has a KVO-compatible sldMoving (assuming sldMoving is KVO-compatible, and I'm sure it is possible if you have it didn’t add some funky path different from the traditional ones), and it has value .

Alternatively, you can also do this ...

 [self.sldMoving addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:NULL]; 

Now that you are using KVO, you need to put together a bunch of gotchas, so you should really read the KVO programming guide: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance. html

Now do not be surprised if you see only the observed changes at the first click of the slider, because you will skip all other changes in the values ​​...

+1
source

It's not entirely clear why you want to use the watchValueForKeyPath function, but you can easily attach the change action to the slider using the line of code below:

 [self.sliderElement addTarget:self action:@selector(didChange:) forControlEvents:UIControlEventValueChanged]; 
+2
source

All Articles