Bhavin_m
Bhavin_m

Reputation: 2784

How to implement KVO on UIStepper

i am creating small demo of KVO. As per my knowledge KVO use to notify when property value is changes. I want to implement KVO on UIStepper. I want to notify when user change value of UIStepper. I did some code on that as follows.

ViewController.h

@interface ViewController : UIViewController
{
   IBOutlet UIStepper *stepper;
}

@property NSInteger intToObserve;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController
@synthesize intToObserve;

- (void)viewDidLoad
{
    [super viewDidLoad];
    stepper.minimumValue = 0;
    stepper.maximumValue = 1000;
    [self addObserver:self forKeyPath:@"intToObserve" options:NSKeyValueObservingOptionNew context:nil];
}

- (IBAction)valueChanged:(UIStepper *)sender
{
    double value = [sender value];
    intToObserve = value;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"Notification Received");
}
@end

Problem is that when i hit plus button stepper value is changed but my notification method did not received any notification or even i set break point there but control not goes there. means -(void)observeValueForKeyPath not called.

Please suggest some idea, what i am doing wrong or to work this what is another solution ??

Thanks in adv.

Upvotes: 1

Views: 311

Answers (1)

borrrden
borrrden

Reputation: 33423

You need to change it via the setter method or the notification will not fire. Change

intToObserve = value;

to

self.intToObserve = value;

Upvotes: 1

Related Questions