0

I am making an app where the user can select 1 of 7 different ball colors to play as. Each color I have assigned an id of 1-7 as integers. The ball selection screen and the actual game screen are 2 separate classes and view controllers. What I want to do is create an int called "BallColor" and then whenever the user clicks the button corresponding to the color then BallColor is equal to the id of that color. Then I need that number transferred over to my games class so it will check against my if statements to know which is the correct ball the user wants to play as. I have tried 5 different method and been researching this for about 6 hours straight and cannot find a good tutorial on how it should be done correctly. Can anyone please help me?? I don't want to give up tonight, but I have no other resources to check. Any help at all would be appreciated!

  • Russ
Russ Perlow
  • 7
  • 1
  • 5
  • Have you seen this question? http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – architectpianist Jun 30 '14 at 03:24
  • No I haven't because I was looking for global ints like everyone else in the question which is apparently wrong. It's the right question but the answer isn't working, it won't let me create @property in my Game.h file no matter where I put it I get an error. – Russ Perlow Jun 30 '14 at 03:45
  • Try using `Singleton` or `NSUserDefaults`. These may help you in maintaining the value throughout the app. – Dhrumil Jun 30 '14 at 07:44

1 Answers1

1

When the user taps on the buttons that determines the color, you can set that integer through NSUserDefaults easily.

-(IBAction) blueColorSelected {

                                     // The integer you set here is the color
  [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"BallColor"];

  // Send the user to the other view you mentioned

}

Then, anywhere in your other view, you can retrieve the value you just stored. Simply use the following code wherever necessary

ballColor = [[NSUserDefaults standardUserDefaults] integerForKey@"BallColor"];

NSUserDefaults also retains its data even after you close the app (so you should use this to save any high scores)

tpatiern
  • 415
  • 4
  • 13