Reputation: 51
I have 2 ViewControllers
. 1 is for the game which has a timer. Second one is for the results. After pressing finish button on the game ViewController
it goes to the result. How can I display the results of the timer on a label on the results ViewController
?
This is my code for timer:
//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;
This is my methods of the timer:
- (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{
}
EDIT:
- (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];
}
EDIT 2 SOLUTION:
I figured it out. I did it in an easier way by adding few lines of code to the timer methods to record the results and then pass them in the other 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"];
}
then add this to second ViewController .m
NSUserDefaults *prefs1 = [NSUserDefaults standardUserDefaults];
NSString *sc = [prefs1 stringForKey:@"keyToLookupString"];
timerResults.text = [NSString stringWithFormat:@"tiempo %@", sc];
Thanks for all the help guys :)
Upvotes: 0
Views: 153
Reputation: 2958
You should pass the string value to next controller while pushing to it,
If you are using .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];
For Storyboard
, use this code
- (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
}
}
EDIT
in first view controller .m
file
//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
}
}
In LetterResults6x6.h
@property (nonatomic, retain) NSString *finalTime;
In LetterResults6x6.m
- (void)viewDidLoad {
[super viewDidLoad];
self.labelOutlet.text = self.finalTime;
}
Upvotes: 1
Reputation: 9246
Just pass the value of your timeSec
and timeMin
while pushing resultsViewController
.
add this Properties in your ResultsViewController.h
@property (nonatomic) int timeSec;
@property (nonatomic) int timeMin;
Now in your GameViewController, add the following on this method:-
//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
}
Upvotes: 0