我有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];
感谢所有帮助人员:)
答案 0 :(得分:1)
你应该在推送它时将字符串值传递给下一个控制器,
如果您使用.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;
}
答案 1 :(得分:0)
在推送timeSec
时,只需传递timeMin
和resultsViewController
的值。
在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
}