如何保存NSTimer? [英] How to Save NSTimer?

查看:68
本文介绍了如何保存NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何保存NSTimer吗?我需要显示玩家完成游戏的时间.请提出建议.

Can anybody say me how to save NSTimer ? I need to display that, at how much time player completed his/her game. please suggest.

谢谢.

推荐答案

您可以这样操作..

int currentTime;

BOOL isGameOver;

 - (void)viewWillAppear:(BOOL)animated 
    {
       isGameOver=FALSE;
       [self start];
       [super viewWillAppear:YES];
    }

- (IBAction)start{

    currentTime = 0;// SET 60 SECODE AS STATICULLY YOU CAN SET YOUR OWN TIME
    lbl=[[UILabel alloc]init];
    //creates and fires timer every second
    myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain];
}

-(IBAction)gameOverAction
{
    isGameOver=TRUE;

//your game action Whenever its game over this method fire and Set One Bool value in this method like
}

-(void)showTime{



        if(isGameOver==TRUE)
        {

            [myTimer invalidate];
            myTimer = nil;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Game Over"
                                                            message:[NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime]
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];

        //you can save this [NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime] in nsuserdefoult or database like bellow

          NSString *myGameLastTime =[NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime] ;
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          [defaults setObject:myGameLastTime forKey:@"gameoverTime"];

          [defaults synchronize];

         //now you can get this time anyplace in your project like [[NSUserDefaults standardUserDefaults] valueForKey:@"gameoverTime"]


        }

        currentTime++;
        lbl.text = [NSString stringWithFormat:@"%.2d", currentTime];

    NSLog(@"my lable == %@",lbl.text);


}

希望它能为您提供最好的帮助

Hope its help you all the best

这篇关于如何保存NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆