1

I'm using a NSTimer in a NSObject class (Timer), that fires a method, that increments a variable and fires a method in a View Controller (InfoViewController). (Confused ;-D )

Everything shows up on the screen and and the timer is started correctly. The variable f.hunger updates and shows the correct value (when using printf("%f", f.hunger) ) but the UIProgress bar doesn't update itself. f.hunger's starting value is 1.

If anyone can help me, point out where I've made an obvious mistake or got a suggestion it would be most appreciated as it's been doing my head in for a good few hours now. I've added some sample code below to show what I'm doing a bit more clearly.

Cheers everyone. :-D

Timers Class

- (void)startHungerTimer
{
 if(hungerTimer.isValid == NO)
 {
  infoVC = [[InfoViewController alloc] init];
  self.hungerTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self     selector:@selector(incrementHungerTimer:) userInfo:nil repeats:YES];
 }
}

- (void)incrementHungerTimer:(NSTimer *)aTimer
{
    f.hunger -= 0.01;
    [infoVC updateHungerProgress];
}

Info View Controller

- (void)updateHungerProgress
{
 Functions *f = [Functions sharedFunctions];
 hungerBar.progress = f.hunger;        
}

- (void)loadView
{
    hungerBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    [hungerBar setFrame:CGRectMake(17.0, 30.0, 160.0, 10.0)];
    hungerBar.progress = f.hunger;
    [bView addSubview:hungerBar];
    [hungerBar release];
}
Baza207
  • 2,123
  • 1
  • 22
  • 40
  • 2
    Have you confirmed hungerBar.progress is correctly initialized / hooked up through IB (if you used IB)? – Sam Dec 14 '10 at 03:39
  • Yeah I've initialized hungerBar above in -(void)loadView{}. Sorry should have mentioned that. :-) – Baza207 Dec 14 '10 at 03:46
  • Does setting hungerBar.progress via an arbitrary literal update the bar, e.x. hungerBar.progress = 0.50f; – Sam Dec 14 '10 at 04:29
  • Umm just tried setting hungerBar.progress to 0.50f when updateProgressBar method gets called and it doesn't do it. It's like for some reason in that method it's not allowing me to update UIProgressView. :( – Baza207 Dec 14 '10 at 14:16

1 Answers1

0

My guess, given your question and comments, is that your "hungerBar" outlet isn't connected to the progress bar in Interface Builder.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Cheers for the suggestion, but I'm not using Interface Builder. I'm doing it all in the code. :-) – Baza207 Dec 14 '10 at 15:24
  • Then I'd suggest posting the code you used to create the progress bar. – Joshua Nozzi Dec 14 '10 at 17:55
  • I've edited the post above to add in the -(void)loadView showing the part of the code where I create the UIProgressView. Hope this shed more light on this. :-) – Baza207 Dec 14 '10 at 18:55
  • Okay, it does look like everything should be fine there, given you maintain your reference to hungerBar. – Joshua Nozzi Dec 14 '10 at 18:57
  • Nooo! I was hopping there was going to be something wrong. Thanks for your help anyway. – Baza207 Dec 14 '10 at 19:05
  • Is there any other code you haven't posted that works with hungerBar? – Joshua Nozzi Dec 14 '10 at 19:18
  • I've got a refreshAttributes method in Info View Controller that has hungerBar.progress = f.hunger; in it. This gets called every time the view is shown. This works fine, so if you go in and out out of the view it refreshes, but not in realtime when you stay looking at it. – Baza207 Dec 14 '10 at 19:25
  • Is the errant update call being done on a separate thread? If so, try using -performSelectorOnMainThread:withObject:waitUntilDone: – Joshua Nozzi Dec 14 '10 at 19:30
  • Hmmm... if you don't know what I mean then it's unlikely you're doing it. :-) UI parts are not generally thread-safe so they have to be updated on the main thread, even if the thing that updated them is running on another. Unless you specifically wrote code to spawn another thread to handle computation, you probably don't need to worry about that possibility. – Joshua Nozzi Dec 14 '10 at 19:42
  • I'll look into it but I don't think I'm doing that. I've done a work around by making another timer that calls refreshAttributes. I'm creating it when the view appears and invalidating it when the view disappears. It might end up being a bit of a resource hog as I don't want to add to many timers than necessary but it makes it work. Thanks a million for all you help and suggestions. :-D – Baza207 Dec 14 '10 at 20:02