views:

41

answers:

1

Blarg!

I'm trying to develop a game with zelda-like qualities. (i.e. When the PC hits the edge of the screen, the screen changes to the next view, and the PC's position restarts at the appropriate edge.)

The problem I'm having is that I don't want to have multiple View Controllers for each level-segment, because all of the data/functionality exists in the original "LevelView" controller. How do I retain the code from this "LevelView" controller, while only switching NIB files? (i.e. The only classes that I want to be there are the AppDelegate, "LevelView", "ItemView", etc.) I don't want to have to re-create a view controller for each NIB file in the game.

I appreciate any help you can offer! Thank you very much! :D

+1  A: 

Create a NIB file that has just the new view in it. Change the class of the File's Owner in the NIB file to the class of the LevelView controller. When you want to load a new NIB file, call the following code from the LevelView controller:

NSArray *topLevelObjects = [[[NSBundle mainBundle] loadNibNamed:@"Put your nib file name here" owner:self options:nil];
UIView *newLevelView = [[topLevelObjects objectAtIndex:0] retain];

What is this doing? When you call loadNibNamed, it returns to you an array of the top-level objects in the NIB file. The "File's Owner" and "First Responder" proxy objects don't count as top-level objects. So, you'll get an array with just the top-level view in the NIB file. You pass self as the file's owner so that any outlet connections you make between the subviews of the top-level view and the "File's Owner" in interface builder get connected.

If you connect the top-level view to an outlet in your view controller, you don't need to do anything with the array loadNibNamed returned. Just ignore it and it'll get released automatically (it gets returned to you with a retain count of 0).

Now, I think doing things this way is a bad idea. I think there are better ways to design your app. For example, it probably makes more sense to create a data file (a property list or XML file for example) that describes the levels than to put all of the levels into NIB files. But, if you really want to do this, the stuff above should get you started.

Jacques
Hmm... when I put just the top half of the code in, it still loads the second view even without necessarily calling it from the array. (I put the code inside of the "ViewDidLoad" method, perhaps this is why?) I like this approach of being able to throw my NIBs into an array, so I can call the appropriate one for each "Map Segment" the character is supposed to be in. Again, why is the new view loading with even only half of your code written? Thank you very much for your help so far! :D
Ah got it! Had the second view attached as a "View" outlet to the view controller. I also added the "self.view = newLevelView" code beneath the second line of what you wrote, because the compiler was complaining about that variable being unused. Thanks again for your help! :D
Hmm.... So each view is contained within its own array... each array can be archived as an object within a Dictionary... then call the correct dictionary object for whichever level-segment the character is in? Is this logic right...?