0

当我按下停止按钮停止计时器时,它只是重置为原始时间并再次开始倒计时。我到处寻找,我发现的只是“无效”而且它不起作用。我希望当我点击停止时时间停止并且标签显示原始时间。我还关闭了自动计数,所以我可以尝试释放,它给了我一个错误:

0x10e20a5: movl 16(%edx), %edx EXC_BAD_ACCESS (代码=2, 地址=0x10)

NSTimer *rockettTimer;
int rocketCount;

@interface FirstViewController ()
@property (strong, nonatomic) IBOutlet UILabel *rocketTimer;

- (IBAction)stopButton:(id)sender;
- (IBAction)startButton:(id)sender;

@end

@implementation FirstViewController
@synthesize rocketTimer;

-(void) rocketTimerRun{
    rocketCount = rocketCount - 1;
    int minuts = rocketCount / 60;
    int seconds = rocketCount - (minuts * 60);
    NSString *timerOutput = [NSString stringWithFormat:@"%d:%.2d", minuts, seconds];
    rocketTimer.text = timerOutput;
}

- (IBAction)startButton:(id)sender {
    rocketCount = 180;
    rockettTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(rocketTimerRun) userInfo:nil repeats:YES];

- (IBAction)stopButton:(id)sender {
    [rockettTimer invalidate];
    //[rockettTimer release];
}

- (void)viewDidLoad
{


    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setRocketTimer:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

 - (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}


@end
4

2 回答 2

2

我想你正在多次按下开始按钮。每次调用 startButton 函数时,它都会创建一个新计时器并忘记旧计时器。[rockettTimer invalidate]为了防止这种情况,我建议您在创建新计时器之前立即放置一个。

您还应该将 RockettTimer 初始化为 nil。更好的是,您应该将其设为该类的成员变量,因为现在您将无法拥有该类的多个实例。

于 2012-10-21T05:02:59.147 回答
-1

您应该尝试使用另一种方法来初始化您的计时器。并在 viewDidLoad 中执行:

于 2012-10-22T03:38:28.230 回答