How to Save NSTimer?

2019-08-23 02:20发布

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

Thanks.

4条回答
来,给爷笑一个
2楼-- · 2019-08-23 02:42

You don't need to use NSTimer. I suggest you using NSDate

When the game starts:

startDate = [NSDate date];

when the game is over:

endDate = [NSDate date];
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];

so now you have time spent and can use it however you like. You can save it in NSUserDefaults just like ordinary double value

查看更多
霸刀☆藐视天下
3楼-- · 2019-08-23 02:51

U should do some research before ask such questions, its easy to figure it out with google, it will take something like 20 min max,

NSUserDefaults *YourTimerName=[NSUserDefaults standardUserDefaults];
[defaultsPassword setObject:YourTimerName forKey:@"key1"];
[defaultsPassword synchronize];

and then u can recover it like that

Nsstring *str=[[NSUserDefaults standardUserDefaults]objectForKey:@"key1"];

but you need to make your timer Nsstring format firstly and then save it

查看更多
该账号已被封号
4楼-- · 2019-08-23 03:01

you can do like this way..

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

查看更多
Root(大扎)
5楼-- · 2019-08-23 03:03

NSTimer is just a source of periodic time events, there’s not much to save. If you use it to keep track of time in your game, there has to be a variable that keeps the total time. Save that into NSUserDefaults or somewhere.

查看更多
登录 后发表回答