Iphone 如何节省时间?

Iphone 如何节省时间?,iphone,ios,cocoa-touch,nstimer,Iphone,Ios,Cocoa Touch,Nstimer,有人能告诉我如何拯救N时代吗? 我需要显示玩家完成游戏的时间。请建议 谢谢。NSTimer只是周期性时间事件的一个来源,没有什么可以保存的。如果你用它来记录游戏中的时间,那么必须有一个变量来记录总时间。将其保存到NSUserDefaults或其他地方。NSTimer只是周期性时间事件的一个来源,没有什么可保存的。如果你用它来记录游戏中的时间,那么必须有一个变量来记录总时间。将其保存到NSUserDefaults或其他地方。您不需要使用NSTimer。我建议您使用NSDate 游戏开始时: sta

有人能告诉我如何拯救N时代吗? 我需要显示玩家完成游戏的时间。请建议


谢谢。

NSTimer
只是周期性时间事件的一个来源,没有什么可以保存的。如果你用它来记录游戏中的时间,那么必须有一个变量来记录总时间。将其保存到
NSUserDefaults
或其他地方。

NSTimer
只是周期性时间事件的一个来源,没有什么可保存的。如果你用它来记录游戏中的时间,那么必须有一个变量来记录总时间。将其保存到
NSUserDefaults
或其他地方。

您不需要使用
NSTimer
。我建议您使用
NSDate

游戏开始时:

startDate = [NSDate date];
游戏结束后:

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

所以现在你有时间了,可以随心所欲地使用它。您可以将其保存在
NSUserDefaults
中,就像普通的
double
值一样,您不需要使用
NSTimer
。我建议您使用
NSDate

游戏开始时:

startDate = [NSDate date];
游戏结束后:

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

所以现在你有时间了,可以随心所欲地使用它。你可以将它保存在
NSUserDefaults
中,就像普通的
double
value

你应该在问这样的问题之前做一些研究,用谷歌很容易搞清,最多需要20分钟

NSUserDefaults *YourTimerName=[NSUserDefaults standardUserDefaults];
[defaultsPassword setObject:YourTimerName forKey:@"key1"];
[defaultsPassword synchronize];
然后你就可以这样恢复它

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

但是你需要先把你的计时器做成字符串格式,然后保存它。在问这些问题之前,你应该做一些研究,用谷歌很容易就搞定了,最多需要20分钟

NSUserDefaults *YourTimerName=[NSUserDefaults standardUserDefaults];
[defaultsPassword setObject:YourTimerName forKey:@"key1"];
[defaultsPassword synchronize];
然后你就可以这样恢复它

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

但您需要先将计时器设置为Nsstring格式,然后将其保存。

您可以这样做

int当前时间

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);


}

希望它能对你有最大的帮助

int当前时间

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);


}

希望它对你有最大的帮助

你是说你想知道用户玩游戏花了多少时间吗?是的,之后我必须在排行榜上显示这段时间。我刚刚测试了我的回答代码,结果非常完美,我不知道为什么有人会给出减号请求,但天使我只是请求你实现我的答案代码希望它能帮助你。。。thx@Angelthx快乐编码…)你的意思是你想知道一个用户玩游戏花了多少时间?是的,然后我必须在排行榜上显示这段时间。我刚刚测试了我的回答代码,它工作得很好,我不知道为什么有人会给出减数要求,但angel我只是要求你实现我的回答代码希望对你有帮助。。。thx@Angelthx快乐编码…)好吧,但我是ios新手,你能告诉我如何保存吗?我试图在nsUserDeafolts中节省时间,但输出为“0”。好的,但我是ios新手,你能告诉我如何节省时间吗?我试图在nsUserDeafolts中节省时间,但在输出中给出“0”。