我一直在研究如何做到这一点,没有人有答案。
我正在同一视图上创建一个具有 5 个计时器的应用程序。我需要创建一个从“15:00”(分钟和秒)开始倒计时的计时器,以及另一个从“2:58”(分钟和秒)开始倒计时的计时器。15 分钟计时器不应重复,但应在达到“00:00”时停止所有其他计时器。“2:58”计时器应该重复,直到“15:00”或“游戏时钟”达到 0。现在,我已经废弃了几乎所有的代码,我正在研究“2:58”重复计时器,或“火箭计时器”。
有谁知道如何做到这一点?
编辑 2:我的模拟器甚至不会运行应用程序,所以我目前不知道计时器是否真的在工作,但从我之前的尝试来看,它没有工作。它没有以我想要的格式显示,并且按 2 倒计时(从它最后一次实际工作开始)。问题还在于,我对 Objective-C 并不流利。我可以像其他人一样整天编写伪代码,但我无法将我想要的东西放入代码中,因为我不完全理解 NSTimer。
编辑:
在我的输出中,我收到此错误“在抛出 'NSException' 的实例后调用终止”
这个错误?
这是我的代码:
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
//Rocket Timer
int totalSeconds;
bool timerActive;
NSTimer *rocketTimer;
IBOutlet UILabel *rocketCount;
int newTotalSeconds;
int totalRocketSeconds;
int minutes;
int seconds;
}
- (IBAction)Start;
@end
和我的.m
#import "FirstViewController.h"
@implementation FirstViewController
- (NSString *)timeFormatted:(int)newTotalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
return [NSString stringWithFormat:@"%i:%02d"], minutes, seconds;
}
-(IBAction)Start {
newTotalSeconds = 178; //for 2:58
newTotalSeconds = newTotalSeconds-1;
rocketCount.text = [self timeFormatted:newTotalSeconds];
if(timerActive == NO){
timerActive = YES;
newTotalSeconds = 178;
[rocketTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerLoop) userInfo:nil repeats:YES];
}
else{
timerActive = NO;
[rocketTimer invalidate];
rocketTimer = nil;
}
}
-(void)timerLoop:(id)sender {
totalSeconds = totalSeconds-1;
rocketCount.text = [self timeFormatted:totalSeconds];
}
- (void)dealloc
{
[super dealloc];
[rocketTimer release];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
timerActive = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end