如何在xcode中的标签上显示计时器结果?

时间:2015-02-06 09:58:00

标签: ios objective-c xcode timer

我在我的应用中添加了一个简单的计时器。此时计时器刚刚从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'的方法 - (无效)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 

2 个答案:

答案 0 :(得分:0)

计时器值存储在timeMintimeSec中。您只需获取timeMintimeSec值即可获得计时器值。

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

编辑: 您还可以实现方法stringValue:

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

并调用方法

[yourtimer stringValue];

答案 1 :(得分:0)

创建全局变量

@interface YourViewController ()
{
NSString *timeNow;
}

现在在 timerTick 方法中使用它。

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

最后,将分数显示为

pointsInfo.text = timeNow;