簡體   English   中英

如何在Xcode的標簽上顯示計時器結果?

[英]How to display timer results on a label in xcode?

我在我的應用程序中添加了一個簡單的計時器。 目前,計時器僅從00:00、00:01、00:02等開始計時。 當我完成一個級別時,我會進入結果屏幕,顯示我的分數。 我對此還是很陌生,我想知道如何顯示計時器結果。 這是計時器的實現:

//in .h file
IBOutlet UILabel *GameTimer;
int timeSec;
int timeMin;
NSTimer *timer;

//in .m file
-(void)startTimer1{

//Invalidate function stops 'timer1' before it is restarted

//initializing 'timer1'

timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(timerTick:)
                                       userInfo:nil
                                        repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}





//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer1
{
    timeSec++;
    if (timeSec == 60)
{
    timeSec = 0;
    timeMin++;
}
//Format the string 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
GameTimer.text= timeNow;

}

我試圖在結果屏幕上顯示它,但我得到的只是0(不要介意西班牙語字符串只是說您得到了多少分):

pointsInfo.text = [NSString stringWithFormat:@"¡Felicidades! ¡Has conseguido      la máxima puntuación: %@! Debajo puedes encontrar las respuestas que has   seleccionado:, time results: %@ ",sc, timer1];

編輯:Game1是一個viewController,gameResults1也是如此。 Game1方法://啟動“ timer1”的方法-(void)startTimer1 {

//Invalidate function stops 'timer1' before it is restarted

//initializing 'timer1'

timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(timerTick:)
                                       userInfo:nil
                                        repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}
//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer1
{
timeSec++;

if (timeSec == 60)
{
    timeSec = 0;
    timeMin++;
}
//Format the string 00:00
timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
GameTimer.text= timeNow;
}
//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
    [timer invalidate];
    timeSec = 0;
    timeMin = 0;
    //Since we reset here, and timerTick won't update your label again, we     need to refresh it again.
    //Format the string in 00:00
    timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    // [timeLabel setStringValue:timeNow];
    GameTimer.text= timeNow;
}
// @selector for 'timer1'

我認為在gameresults1中調用game1時,我沒有任何特殊方式。 以外:

@interface Game1Results : Game1 

計時器值存儲在timeMintimeSec 您只需要獲取timeMintimeSec值即可獲取計時器值。

NSString * resultTimer = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
NSLog(@"%@",resultTimer);

編輯:您還可以實現方法stringValue:

-(NSString*)stringValue{ 
    return [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec]; 
}

並調用方法

[yourtimer stringValue];

創建一個全局變量

@interface YourViewController ()
{
NSString *timeNow;
}

現在,在您的timerTick方法中使用它。

timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];

最后,將得分顯示為

pointsInfo.text = timeNow;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM