简体   繁体   中英

How to add observer on a UISlider

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

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.

Connect the slider to an IBAction as 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM