0

I have set

[self.progressView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];

if I use self.progressView.progress = 0.1; to change progress, it's ok.

but if I use [self.progressView setProgress:0.1 animated:YES];, the method -observeValueForKeyPath:ofObject:change:context: can't be trigged.

what can I do now? now I can't change [self.progressView setProgress:0.1 animated:YES]; because this code in another lib.

Vincent
  • 141
  • 1
  • 2
  • 15

2 Answers2

0

You're probably out of luck here. You can't control the implementation of UIProgressView. File a bug with Apple if you like, but I wouldn't hold my breath, because...

Conceptually, w/r/t the MVC pattern used in iOS and OS X, this is not something you should be doing in the first place. Views should be the "observers", not the "observees". You should find another way to achieve this.

Yes, you could probably hack this into submission by swizzling the implementation of -setProgress:animated: to call -willChangeValueForKey: and -didChangeValueForKey: before and after calling the original implementation, but do yourself a favor and find another way.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
0

I'm run into this issue too.

I'm using the following code to solve this.

CGFloat progress = 0.3;//this is just a example.
if (progress < self.progressView.progress) {
    self.progressView.progress = progress;
}
else
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.progressView setProgress:progress animated:YES];
    }completion:^(BOOL finished) {
        //avoid key-value observer invalid
        self.progressView.progress = progress;
    }];
}

This is working for me now. Hope it could help someone.

Yingsong
  • 76
  • 3