Still learning (pt2) - How to change views

Discussion in 'Public Game Developers Forum' started by wastedyuthe, Jan 30, 2009.

  1. I had some great help with the first thread on forcing landscape, and now I am stuck on the next part of my project. As I have said before, I am waiting for my book to arrive (I have actually ordered another one today too), but until it does, I have been tinkering. I have got to the stage where I can create a single view, with useable buttons (my first project was a simple calculator). So I thought I would start on the main menu and settings views for what will be my first game. I have indeed created all the graphics for them, and have my main menu appearing when I start the app, with buttons for Play, Settings, Info- which have been allocated UIAction methods in the ControllerView files. I have created ControllerView files for the settings menu too, and I have defined the buttons and set a method for each, but so far I have nothing written in any of the button methods. Because what I want to do, for the moment at least, is allow the settings menu to appear when I tap the Settings button. And that's where I am stuck. I am finding it very hard to find any info online on how to actually set up different views and alternate between them. I would very much appreciate someone helping me to understand and answer these questions...

    1)Do you use the one nib (.xib) file for all views, or do you create a seperate one for each? I can create a new nib file, but don't know if you are supposed to just use the one.

    2)What command would I type in the UIAction method for the Settings button, in order to change to the Settings menu view?

    Thank you in advance (I wish this bloomin' book would turn up fast!)
  2. Schenk Studios

    Schenk Studios Well-Known Member

    In brief, you'll need to create a new viewcontroller class for your settings, add a ViewController from the library to your .xib window. Add another view to that. Connect the controller to view. Set the controller to the class that you just made. Create an IBOutlet for that class in your menu and hook it up with a blue connector in IB. Then in your button action method call a line something like this. [self presentModalViewController:settingsViewController animated:Yes];
  3. Haha! That whole paragraph just flew straight over my head so fast I didn't even see it!

    It'll take a while to digest that info and look into those settings in the IB. But basically you're saying I can use the same xib file anyway, right?
  4. Schenk Studios

    Schenk Studios Well-Known Member

    Correct, you'll be adding two things to it from the library. A UIViewController, and a UIView.
  5. Aha! I was trying to find out how to add the view to the new ViewController. Of course! UIView in the Windows, Views, and Bars section of the library- drag it ONTO the new settings ViewController. Got it. Now I got to go through the rest.
    What do you mean by 'connect the controller to view'?
  6. Schenk Studios

    Schenk Studios Well-Known Member

    I'll post a youtube vid in a bit that should make things clearer. Mixing the graphical programming of IB and the text programing of Xcode can make explanations tricky. :-D
  7. #7 wastedyuthe, Jan 30, 2009
    Last edited: Jan 30, 2009
    I hear ya! Thanks very much! For your information, in your quote below, I have marked the sections I am having trouble with in bold. I can do/have done the rest (although, is there anything I need to add to that line I call in the button method? It comes up with an error when I type that in, although that may be because I haven't done the rest of course).

  8. Schenk Studios

    Schenk Studios Well-Known Member

  9. WOW! Thank you so much for that vid! Seeing it done makes it so much easier. There were a few things that you did in IB, such as dragging the blue line to the Files Owner etc, that I didn't know. Also, when I tried, I dragged the UIView ONTO the SettingsViewController rather than next to it and dragging the blue line to it.

    What you did in a few minutes will take me about 20, so I'll finish off after dinner. But I'll say thank you once again, as your tutorial video has helped immensely. Much appreciated :)
  10. Schenk Studios

    Schenk Studios Well-Known Member

    Glad I could help. If you run into problems, check capitalization and check your connections in IB.
  11. Hehe! Funny you should mention that- I had to start over. I named my class with a small 's' (settingMenuViewController) and it caused problems when you have commands using the lower case version which conflicted. I Tried to change names over, and still got error messages, so I am in the process of starting again. Shouldn't take long now... hopefully :D

    When I get to the animation part, is there a way to choose which side the new view comes in from?
  12. DONE IT!! Looks great! I'll go over what we did a few times so I understand exactly how it was done (I more or less understood everything I think).

    Thanks again. Maybe this thread will help others learn these things too. There aren't any sample codes that show you this particular function!
  13. Schenk Studios

    Schenk Studios Well-Known Member

    Take a look at a Utility app. New Project >> iPhone >> Utility (instead of view based). That has plenty of code to dig through!
  14. #14 wastedyuthe, Jan 31, 2009
    Last edited: Jan 31, 2009
    Yay! My book has finally arrived now! ("Beginning iPhone Development" by Dave Mark and Jeff LaMarche)

    I have skipped forward to the chapter on handling multiple views, and its example project actually uses multiple nib files. I know it can obviously be done with one now (thanks to the wonderful help I have been given so far), but I am now wondering- what would be the advantage of each view having their own nib (.xib) file over them all sharing one as we have done? Is there an advantage, does it depend on what those views do, or is it just personal preference?

    If it's a case of using more nib files to help keep them organized, I thought of maybe using one for the menus, and another for the actual gameplay. What do you think?
  15. Schenk Studios

    Schenk Studios Well-Known Member

    It is a matter of organizational preference and also a matter of when you want that xib to load. Everything that you put in the default xib will be loaded during the application startup, "eager approach". Optionally you can load views after the application has started up. This will cause the app to start up faster, (since it isn't loading everything from the get go) but it may cause pauses when you're users are going from view to view since it has to do the loading then "Lazy Approach". In the end, chances are either approach will suffice so pick one and run with it!
  16. Fantastic advice, thanks. So, with a small game, you may as well load it all together with one xib file straight away. But with larger games it may be better to seperate sections into seperate xib files.
    I think with my first game, one xib file will be adequate enough to load it all at startup as it won't be particularly complex enough to warrant splitting into further loads.
    Sound about right?
  17. Schenk Studios

    Schenk Studios Well-Known Member

  18. #18 wastedyuthe, Jan 31, 2009
    Last edited: Feb 1, 2009
    Thanks (hehe! Are you getting fed up of me saying that yet? You're just too helpful my friend). Downloaded the free screencast episode introducing Xcode and IB etc. But I won't be buying the book as I already have the one delivered today plus another on the way. The one you gave a link to does look good though.

Share This Page