27

試験が行われている場所でアプリケーションを作成しているので、試験が始まると、その時間から開始する必要があります。たとえば、30 分で、29:59 のように短縮する必要があります。

どうすればこれを実装できますか?

誰でも私が従うことができるサンプルの例または簡単なステップバイステップのチュートリアルを教えてください。

4

1 に答える 1

67

このコードは、カウントダウン タイマーを作成するために使用されます。

.h ファイルのコード。

@interface UIMyContoller : UIViewController {

NSTimer *timer;
    IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;

-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;

@end

.m ファイルのコード。

@implementation UIMyController
@synthesize myCounterLabel;

int hours, minutes, seconds;
int secondsLeft;

- (void)viewDidLoad {
    [super viewDidLoad];

    secondsLeft = 16925;
    [self countdownTimer];
}

- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ) {
        secondsLeft -- ;
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    } else {
        secondsLeft = 16925;
    }
}

-(void)countdownTimer {

    secondsLeft = hours = minutes = seconds = 0;
    if([timer isValid]) {
        [timer release];
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    [pool release];
}

これがお役に立てば幸いです。

于 2013-06-17T12:33:10.707 に答える