So im conceptually stuck building my app. I’ve read lots of forums but I think I need to try a new route. Heres what im trying to do.
I created a game where each level loads a different string for the user to play with. Right now the strings for the levels are stored in a plist. There are 5 categories (arrays) each of which contains 20 different strings (ie 20 levels) so there are 100 levels total.
When the user starts the game they reach the “LevelSelect” view which consists of a picker. The picker has two components. the first component is a list of categories, and the second component is the list of levels in that category. The user selects which of the levels they want to play, then press a “Start” button to bring up the “GamePlay” view (an entirely different set of .h, .m, and .xib files).
MY PROBLEM: I dont know how to make the selected level appear on the screen in the Gameplay class. I have all the machinery of the game working, so if I create a string in the viewDidLoad, the game appears to work fine, but I want to have the string for that level come onto the screen when that level is selected and the user presses start.
MY QUESTION: How do I pass the string from LevelSelect to GamePlay?
Thanks for any help you can offer!
If I understand your question correctly,
LevelSelectandGamePlayare both subclasses ofUIViewController. The user chooses the level he wants to play through theLevelSelectview controller. It sounds like you already know how to display the view for theGamePlayview controller. You just don’t know how to pass a string to that view controller before you display it.When the user presses the start button, I presume that you’re currently constructing and presenting your
GamePlayview controller with code that looks something like this:Your goal is give your
GamePlayview controller alevelStringproperty that you can set like this:where
self.selectedLevelStringis the string for the level the user selected in the picker.To achieve this you’ll need to declare the property in your GamePlay.h file like this:
In your GamePlay.m file you’ll need to synthesize this property and release it in your dealloc method like this:
This gives your GamePlay controller a
levelStringproperty, which you can set from yourLevelSelectview controller as shown above. Then within any method in theGamePlayview controller, you can access this string usingself.levelString.By the way, my example code for
startButtonPressed:above presumes that you have a property on yourLevelSelectview controller calledselectedLevelString. You can declare this property the exact same way we declared thelevelStringproperty onGamePlay. You can set this string as appropriate when the user selects a level inpickerView:didSelectRow:inComponent:.