문제

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

도움이 되었습니까?

해결책

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.

다른 팁

[_minu addObserver:self forKeyPath:@"currentTime" options:NSKeyValueObservingOptionNew context:NULL];

It should be currentTime instead of tempsPublic.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top