2

I've got a GameScreen.m file like (this is a simplified piece of the code):

- (IBAction) onCellClick:(id) sender
{
  points +=1;
  self.myScore.text = [[NSNumber numberWithInt: points] stringValue]; 
 //myScore is a label in GameScreenViewController xib
} 

That is, upon clicking a cell in the view, it will increase a text label by 1. So far so good.

then, in the same code, I've got a timer:

- (void) startTimer
{ 
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(updateCounter:)
                                   userInfo:nil
                                    repeats:YES];
}

its updateCounter method is:

- (void) updateCounter:(NSTimer *)theTimer
{
     int seconds;
     static int count = 0;
     count +=1;
     timeElapsed = [[NSString alloc] initWithFormat:@"%d", seconds + count];
     self.time.text = timeElapsed;
     //time is a label in GameScreenViewController xib
}

the thing is that "time" label is not updated (1 sec each time) in this case. I've inserted an AlertView to check if the startTimer method is valid and correctly called, and it actually is (it shows an annoying alertview each second with the timeElapsed value). However, I can' get the time label value to be changed.

Why is my score label updated upon action, while time label isn't updated every second? Is there any way I can update it without including my code in the ViewController?

//note: my coding splits into three files: the appDelegate flips screens and sends values among them; my viewControllers just the windows and, finally, my GameScreen class manages all the processes. From the xib, File's Owner is connected to the ViewController, and the view is connected to GameScreen class.

Thanks a lot for any feedback, please feel free to ask for any piece of additional code needed.

Mario
  • 55
  • 8
  • You've checked that `self.time` is definitely correctly wired up (and hence not `nil` at runtime) and you've not moved the timer onto a separate thread or anything like that? – Tommy Nov 22 '11 at 12:29
  • actually, I did this as well in the (IBAction) method: self.time.text = @"DO SOMETHING, DAMMIT!"; it showed the message, so it's well instatiated and connected in the view... – Mario Nov 22 '11 at 12:30
  • Did you try `[self.time setNeedsDisplay]`? – Hot Licks Nov 22 '11 at 13:17
  • How about to add a line of NSLog("%@", timeElapsed); after self.time.text = timeElapsed; – mr.pppoe Nov 24 '11 at 09:41

2 Answers2

0

You have to do that (UI related operations) in main thread.

Instead of the line,

 self.time.text = timeElapsed;

do as follows:

[self.time performSelectorOnMainThread:@selector(setText:) withObject:timeElapsed waitUntilDone:NO];

Edit:

- (void) updateCounter:(NSTimer *)theTimer
{
     //int seconds;
     static int count = 0;
     count +=1;
     NSString *timeElapsed1 = [[NSString alloc] initWithFormat:@"%d", count];
    [self.time performSelectorOnMainThread:@selector(setText:) withObject:timeElapsed1 waitUntilDone:NO];
    [timeElapsed1 release];
     //time is a label in GameScreenViewController xib
}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

I have gone through an UGLY walkaround. It works to some extent, but I went through such a crappy fix that I'm too embarassed to share...

Basically, I moved my timer directly to ViewController since I want it to be fired upon view load and can't get it to work with a call from ViewController's -(void)viewDidLoad to GameScreen's -(void) startTimer. All other stuff involving both methods is, pretty much, duplicated (ok, not duplicated, let's say 'polymorphed' since I handle some variables to fire them).

It seems my GameScreen.m IBActions can only fire other methods within my GameScreen.m, not on GameScreenViewController.m. Thus, I'm handling my buttons' behavior on GameScreen.m and, on GameScreenViewController.m, I just handle 'automatic' stuff; that is, anything not depending on user interaction. It made me have some IBOutlets duplicated depending on input/output needed so I guess that, since it's now working, you can't tell the difference if you don't go under the hood...

Thanks everyone for their feedback though.

Mario
  • 55
  • 8