2010-09-02 67 views
0

HI由于警报和定时器存在问题,问题是:有关定时器,设备和模拟器的问题

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]; 
} 
} 

一切都会在模拟器罚款,但在设备上的警报不显示和应用terminates.On控制台上面的NSLog(@“的机会后有一个消息“EXC_BAD_ACCESS” --- “)。

回答

1

这可能不是走错了唯一的事情,但otherButtonTitles的清单必须在零结束,如:

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

这是因为it takes an indefinite number of arguments,而在真正的C的方式,接收器不会隐知道长度,所以它继续尝试将相邻数据解释为字符串指针,直到找到0值。 (对于像NSString的+stringWithFormat:方法这样的方法来说,这是不必要的,它知道格式字符串中出现多少个格式说明符会有多少更多的参数。)它真的是一个不幸的巧合,它也不会在模拟器中崩溃。

+0

哦!我忘了以零结束其他按钮。 Thanx.我从来没有想到这个错误会在这里,因为它是一个愚蠢的错误。 – Jack 2010-09-03 05:29:13