Reputation: 57
I'm starting to learn objective-c and my first app would be simple. I created a class to handle a timer. It has a property currentTime, a startAndPause method and a stopTimer method.
I'm initialising my Timer in the viewDidLoad method of my ViewController as :
_minu = [[KUUMinuteur alloc] initWithDuration:@70];
Then, I want my ViewController to observe my _minu.currentTime property changes. So I did this :
[_minu addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew context:NULL];
And in my viewController, I wrote this method but it never triggers :
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"Changing !!");
}
I don't know what am I doing wrong. :(
(My App is a single View app.)
EDIT : forgot to translate tempsPublic to currentTime
Upvotes: 3
Views: 115
Reputation: 437402
You probably are never changing the currentTime
property of KUUMinuteur
.
I wonder if you are trying to directly update _currentTime
ivar that backs your currentTime
property. When changing the value of a property you do not want to use the ivar (other than the init
, dealloc
, and custom setter methods, if any):
_currentTime = ...; // wrong
You want to use the setter, e.g.
self.currentTime = ...; // right
or
[self setCurrentTime:...]; // right
Make sure you use the setter or else the key-value notification will not take place.
See the Use Accessor Methods to Set Property Values section of the Advanced Memory Management Programming Guide. Or see the Automatic Change Notification section of the Key-Value Observing Programming Guide.
Upvotes: 1
Reputation: 1964
[_minu addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew context:NULL];
It should be currentTime
instead of tempsPublic
.
Upvotes: 0