如何在下一个“ViewController”显示计时器结果

How to display Timer results on next `ViewController´

我有 2 个 ViewControllers。 1 用于有计时器的游戏。第二个是为了结果。在游戏 ViewController 上按下完成按钮后,它会进入结果。如何在结果 ViewController?

的标签上显示计时器的结果

这是我的计时器代码:

//these are initialized in games ViewController.h
IBOutlet UILabel *GameTimer;
int timeSec;
int timeMin;
NSTimer *timer;

//game ViewController.m under @implementation
INSString *timeNow;
NSTimer *timer1;

这是我的计时器方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self performSelector:@selector(startTimer1) withObject: nil afterDelay:0.0];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)startTimer1{

    //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:@"Tiempo: %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;
}
//executed after the finish button is pressed
- (IBAction)finishPressed:(id)sender{
}

编辑:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    LetterResults6x6 *nextViewController = [[LetterResults6x6 alloc] initWithNibName:@"ViewController" bundle:nil]; //Let us assume that ViewController is the controller in which you wish to show the result.
    nextViewController.finalTime = timeNow; //here finalTime is a NSString property in ViewController Class
    [self.navigationController pushViewController:nextViewController animated:YES];
}
- (IBAction)finishPressed:(id)sender
{

    [self performSegueWithIdentifier: @"finished" sender: self];

}

编辑 2 解决方案: 我想到了。我通过在计时器方法中添加几行代码来记录结果然后将它们传递到另一个 ViewController

来以更简单的方式做到这一点
- (void)timerTick:(NSTimer *)timer1
{
    timeSec++;
    if (timeSec == 60)
    {
        timeSec = 0;
        timeMin++;
    }
    //Format the string 00:00
    timeNow = [NSString stringWithFormat:@"Tiempo: %02d:%02d", timeMin, timeSec];
    //Display on your label
    //[timeLabel setStringValue:timeNow];
    GameTimer.text= timeNow;
    //code i added
    NSUserDefaults *prefs1 = [NSUserDefaults standardUserDefaults];
    [prefs1 setObject:[NSString stringWithFormat:@"%@",timeNow] forKey:@"keyToLookupString"];
}

然后将其添加到第二个 ViewController.m

NSUserDefaults *prefs1 = [NSUserDefaults standardUserDefaults];
NSString *sc = [prefs1 stringForKey:@"keyToLookupString"];

timerResults.text = [NSString stringWithFormat:@"tiempo %@", sc];

感谢大家的帮助:)

只需在推送 resultsViewController 时传递 timeSectimeMin 的值即可。

将此属性添加到您的 ResultsViewController.h

@property (nonatomic) int  timeSec;
@property (nonatomic) int  timeMin;

现在在您的 GameViewController 中,在此方法中添加以下内容:-

//executed after the finish button is pressed
- (IBAction)finishPressed:(id)sender{
  ResultsViewController *resultViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ResultsViewController"];
   resultViewController.timeMin=timeMin;  //pass the value
   resultViewController.timeMin=timeSec;  //pass the value

  // now push to your ResultViewController

}

你应该在推送到下一个控制器时将字符串值传递给它,

如果您正在使用 .xib

ViewController *nextViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; //Let us assume that ViewController is the controller in which you wish to show the result.
nextViewController.finalTime = timeNow; //here finalTime is a NSString property in ViewController Class
[self.navigationController pushViewController:nextViewController animated:YES];

对于Storyboard,使用此代码

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[ViewController class]]) {//Let us assume that ViewController is the controller in which you wish to show the result(i.e., Second controller).
        nextViewController.finalTime = timeNow; //here finalTime is a NSString property in ViewController Class
    }
}

编辑

在第一个视图控制器中 .m 文件

//executed after the finish button is pressed
- (IBAction)finishPressed:(id)sender{
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender     
{ 
    if ([segue.destinationViewController isKindOfClass:[LetterResults6x6 class]]) { 

    nextViewController.finalTime = timeNow; //here finalTime is a NSString property in ViewController Class 
    } 
}

LetterResults6x6.h

@property (nonatomic, retain) NSString *finalTime;

LetterResults6x6.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.labelOutlet.text = self.finalTime;
}