1

UIProgressView progress not updating in iOS8&9 but work in iOS7 even when run in the main thread. My code is the following, hope have a help.

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
}

- (void) handleProgressBar{
    if(self.usedTime >= 300.0)
    {
        [self.timer invalidate];
        self.timer=nil;
        [self submitAns:[NSNumber numberWithInt:-1]];
    }
    else
    {
        self.usedTime += 1;
        CGFloat progress = self.usedTime*(0.0033333333);

        [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
        if(self.usedTime>200){
            [self.progressBar setProgressTintColor:[UIColor redColor]];}
    }
}

- (void)updateProgress:(NSNumber *)progress {
    float fprogress = [progress floatValue];
    //self.progressBar.progress = fprogress;
    [self.progressBar setProgress:fprogress animated:YES];
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
Winston
  • 49
  • 8
  • 1
    Do some debugging. Is `updateProgress:` being called with expected values? Is `self.progressBar` set to a non-nil value? – rmaddy Apr 27 '16 at 04:11
  • Use [Debugging View Hierarchies](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html#//apple_ref/doc/uid/TP40015022-CH9-SW2) and check which is value of `fprogress` – Tim007 Apr 27 '16 at 04:20
  • Have you checked your - (void)updateProgress: method get called or not ??? – vikash1307 Apr 27 '16 at 05:00
  • why are you using viewDidapppear to schedule timer? put it in viewDidload – Ketan Parmar Apr 27 '16 at 05:43

1 Answers1

0
@property (strong, nonatomic) UIProgressView    *progressView;
@property (nonatomic)        CGFloat            usedTime;

i put it in viewDidLoad Method

 _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 500,  self.view.bounds.size.width, 50)];
[self.progressView setProgress:0.0];

[self.view addSubview:self.progressView];

 self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];

[![enter image description here][1]][1]//-----------------------------------------------------------------------

- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
    [self.timer invalidate];
    self.timer=nil;
  //  [self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
    self.usedTime += 1;
    CGFloat progress = self.usedTime*(0.0033333333);

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
    if(self.usedTime>200){
        [self.progressView setProgressTintColor:[UIColor redColor]];}
}
}


//-----------------------------------------------------------------------


- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressView setProgress:fprogress animated:YES];
}

//-----------------------------------------------------------------------

Working For ME

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • Don't just post code. Explain what the OP was doing incorrectly and explain how your answer solves the problem. – rmaddy Apr 27 '16 at 04:55
  • PKT, Thanks for your code, but it also not updating the progress bar. The weird thing is that when I set a pointer on handleProgessBar, i can see the progress increasing in debug windows, but on the simulator, nothing to see on progress bar in iOS8 & 9, do you have any idea? – Winston Apr 27 '16 at 07:54
  • in ios9 it is working perfectly as u can see in screen shot – Prashant Tukadiya Apr 27 '16 at 07:57
  • yes, i have tried, run in the simulator, in will count the time, but the progress bar stuck in 0. – Winston Apr 27 '16 at 08:00
  • there might me problem of thread if progress not updating .as you must set progress in main thread and you are doing it correctly . still you got issue is surprising . you have your progress view in xib ? – Prashant Tukadiya Apr 27 '16 at 08:05
  • `if(self.usedTime>200){` ` [self.progressView setProgressTintColor:[UIColor redColor]]; ` try to comment this thing for testing `// [self submitAns:[NSNumber numberWithInt:-1]];` – Prashant Tukadiya Apr 27 '16 at 08:07
  • I have a import a UITableViewCell in this controller, and the progressBar is in here. Is it incorrect? #import .@class Question; . .@interface QuestionTableViewCell : UITableViewCell .@property (strong, nonatomic) IBOutlet UIProgressView *progressBar; .- (void) configCellWithQuestion:(Question*)q withQIndex:..(NSInteger)qIndex; .@end And call it in controller like this:@property (strong, nonatomic) IBOutlet UIProgressView *progressBar; – Winston Apr 27 '16 at 08:49