开发者

Getting variables from one view to another

开发者 https://www.devze.com 2023-02-27 04:13 出处:网络
I realise that there are other topics like this, but none of them really help. I\'m trying to get variables from one view into another, but I have absolutely no idea how.

I realise that there are other topics like this, but none of them really help. I'm trying to get variables from one view into another, but I have absolutely no idea how.

To give some backsto开发者_运维技巧ry, my game is a fruit ninja like game where stuff goes on the screen and you have to slice it. If 3 sprites leave the screen unsliced, the game is over and it flips to the game over view screen with a button to go back. Additionally, this SHOULD go to the "flipSideView", which is the highscore, but my implementation of the flipSideView transition doesn't work. This isn't the main issue, the main issue is that I don't know how to get the score from the game in the mainView (which stores it as an int) into the flipSideView (which has the player name).

The main view changes to the gameOverView through this condition in the tick method (which performs regular checks and methods for the game)

if (lifeCounter < 1) 
{
        gameIsOver = YES;
        [self showInfo:0];
        [self viewGameOverScreen];
}

That goes to the gameOverView, which will sit there until the replay button is pressed with:

- (IBAction)replayAction:(id)sender 
{
        [self.delegate gameOverViewControllerDidFinish: self];
}

gameOverViewControllerDidFinish restarts the game and dismisses the view.

- (void) gameOverViewControllerDidFinish: (GameOverViewController *) controller
{
        [self restart];
        [self dismissModalViewControllerAnimated: YES];
}

The restart method just resets the 3 primary values in the main view (The score, the level, the lives).

As it restarts, the game should take the score and whatever name is stored in the text field in the flipSideView (which can be viewed at any one time gameplay) and store it somehow for future reference. It's not supposed to store it in a file yet because that's next week's task.

I don't wanna post the entire program due to plagiarism issues, but if there are additional parts that might make it easier to understand, I will definitely post them.

Thanks in advance, Christian


Use Search


NSUserDefaults *currentScore = [NSUserDefaults standardUserDefaults];
[currentDefaults setFloat:yourScoreVariable forKey:@"HighScore"];
[currentDefaults synchronize];  

Above to store the score. Below to retrieve the score.

CGFloat fltHighScore;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
fltHighScore = [currentDefaults floatForKey:@"HighScore"];


Suggestion:

You could put the variable in your Controller class,where they both could access.

It is always better to have sharable data in Controller then views if data has to be shared among views.

Update the shareable data through delegate method.


You can use your app delegate for this. You need to declare your variable there, and then you can access this in the following way throughout your whole app:

TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate]; NSString *string = [appDelegate.Placemarks objectForKey:appDelegate.title];

Don't know if it's the best way tough...

Luck with it!

0

精彩评论

暂无评论...
验证码 换一张
取 消