views:

40

answers:

2

Any idea how can i add my controller class as a observer for my View class' slider?

I am trying to achieve this via Observer way: Inside my controller's viewDidLoad method I am calling

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

and in the same controller file i am implementing:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if ([keyPath isEqualToString:@"myView.mySlider.value"]) {
        myView.myImgView.animationDuration = [[change valueForKey:NSKeyValueObservingOptionNew] intValue]; 
    } 
} 

But my app is crashing. Please help

A: 

Connect the slider to an IBActionas you would do with a button. The action is called whenever the slider changes its value. If you need to be notified in another class of changed slider values, send a NSNotification in your IBAction and register for that notification in other classes.

tob
I am trying to achieve this via Observer way:Inside my controller's viewDidLoad method I am calling[self addObserver:myView forKeyPath:@"myView.mySlider.value" options:NSKeyValueObservingOptionNew context:NULL];and in the same controller file i am implementing:- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"myView.mySlider.value"]) { myView.myImgView.animationDuration = [[change valueForKey:NSKeyValueObservingOptionNew] intValue]; }}But my app is crashing. Please help.
Abhinav
Please format that code in a readable fashion.
tob
Inside my controller viewDidLoad==>[self addObserver:myView forKeyPath:@"myView.mySlider.value" options:NSKeyValueObservingOptionNew context:NULL];==== Inside my view controller class ===>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"myView.mySlider.value"]) { myView.myImgView.animationDuration = [[change valueForKey:NSKeyValueObservingOptionNew] intValue]; }}
Abhinav
I am not able to put the code in the readable format when adding comments :( ... sorry about that
Abhinav
then, I think you should edit the question
vodkhang
Abhinav: Moreover, it appears to just repeat the question. There is no need to do that.
Peter Hosey
Thanks it worked.
Abhinav
+1  A: 

I got the problem. I was setting the observer wrongly. Instead of calling the addObserver method on self i am supposed to call it on the object which i need to observe.

Abhinav