有关计时器,设备和模拟器的问题

时间:2010-09-02 10:58:50

标签: iphone ios-simulator nstimer

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

在模拟器上一切正常,但在设备上警报没有显示,应用程序终止。在上面的NSLog(@“机会---”)之后,控制台上有一个消息“EXC_BAD_ACCESS”。

1 个答案:

答案 0 :(得分:1)

这可能不是唯一出错的,但是otherButtonTitles的列表必须以nil结尾,如:

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:方法这样的东西,这不是必需的,它知道格式字符串中出现了多少格式说明符会有多少进一步的参数。)这真的是一个不幸的巧合,它不会在模拟器中崩溃好。