views:

30

answers:

1

HI Frends there is a problem regarding alert and timer. The problem is:

timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0/30 target:self
     selector:@selector(Loop1) userInfo:nil repeats:YES];

timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
     selector:@selector(timrClock) userInfo:nil repeats:YES];

-(void) timrClock
{

long diff = -((long)[self.now timeIntervalSinceNow]);
timrLabel.text = [NSString stringWithFormat:@"%02d:%02d",(diff/60)%60,diff%60];

if(diff >= timeBankCounter)
{
    if(clockTimer != nil)
    {
        [clockTimer invalidate];
        clockTimer = nil;
    }
    targetButton.userInteractionEnabled = NO;
    NSLog(@"RESTART");
    NSLog(@"chance:- %d",[[self appDelegate].chance intValue]);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time Out!" message:@"Your time is over." delegate:self cancelButtonTitle:@"Try Again." otherButtonTitles:@"Quit"];
    [alert show];
    [alert release];
    //[timer invalidate];
}
}

everything is going fine on simulator but on device the alert doesn't show and app terminates.On console there is a msg "EXC_BAD_ACCESS" after the above NSLog(@"chance---").

+1  A: 

This may not be the only thing going wrong, but the list of otherButtonTitles MUST end in nil, as in:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time Out!" message:@"Your time is over." delegate:self cancelButtonTitle:@"Try Again." otherButtonTitles:@"Quit", nil];

This is because it takes an indefinite number of arguments, and in true C fashion, the receiver does not implicitly know the length, so it continues trying to interpret adjacent data as string pointers until it finds a 0 value. (This is not necessary for something like NSString's +stringWithFormat: method, where it knows how many further arguments to expect by how many format specifiers appear in the format string.) It's really an unfortunate coincidence that it's not crashing in the simulator as well.

zem
oh! I forgot to end otherButtonTitles with nil. Thanx.I never expect the error to be here,because its a silly mistake.
Jack