Tuesday, April 21, 2009

iPhone -- Interface Builder, Part 1


Interface Builder is currently the bane of my existence.  I just don't quite understand what I'm doing, and I definitely don't understand HOW it is working.  I'll outline the latest project here, and how I sort of got it working.  I mean, I DID get it working, but I'm not sure about some of the decisions I made and how it will bite me down the road.

I started out by creating a Tab Bar Application based project in Xcode.  I ran it, and it compiled.  Check.  The MainWindow.xib file already has the first view loaded into it, but the 2nd is done via magic.  I wanted to figure out what the magic really was.  The "First View" has this text in a label:  "Optionally move this view into a separate nib file and set the nib name in the First View Controller Attributes."  Well, why not try this?  Let's see if I can replicate this as I type, mistakes and all.

First, I looked at the inspector for the MainWindow.xib file.  File Owner is a class of "UIApplication".  It also has an entry called "App Delegate", and it's class is "TBNB001AppDelegate"  This is the name of my application (TBNB001), and when I made the project, Xcode created TBNB001AppDelegate.h and .m for me automatically.  This is what is assigned to the "App Delegate".  

MianWindow.xib has a Window, but that is pretty uninteresting, I think, because I believe that all iPhone apps need to have 1 window, and that's all.  It's a class of UIWindow, so it hasn't been altered or overridden, etc.

Then we get to the last parent level object, "Tab Bar Controller".  It's type is UITabBarController, but it isn't listed in the Inspector.  That's...odd.  Why?  I mean, I don't plan on overriding or catching any events from the Tab Bar (actually, I do later on, but that is my project specific, and I'll deal with that later).  But shouldn't it have a class of UITabBarController, too?

Under "Tab Bar Controller" is "Tab Bar".  Okay...same thing as the Controller above...it has a type, but no specified Class.  I HAVE to assume here that if the Class is blank, then it is just the same as putting in the default class.

Next under "Tab Bar Controller" is "Selected First View Controller".  Not sure if "Selected" is set from some property I couldn't find, or if it was just called that.  No idea.  It's class is called FirstViewController -- again, this relates to 2 files of the same name + .h and .m that Xcode created for me automatically.  The View Controller has a connection to the view inside of it.  A View Controller must have a default Outlet defined for a view, cause it sure isn't anywhere in the code for "FirstViewController.h" or .m.

Now, look at the structure for "View Controller (Second)"  It's class and type are both UIViewController.  VC1 has a view outlet that is used, but VC2's view is unassigned.  On the last page of the inspector, VC1 has no class outlets defined, but VC2 does, and it is a UIView.

I'm pretty confused, I have to say.

Finally, though, VC2 is set to load from a NIB called "SecondView".  Ahh, that magic part, I guess.  So, I do have another file, and it is called SecondView.xib.  I don't have one for the first, but I do for the second.  The first is defined in place in the MainWindow.xib file, under the Tab Bar Controller -> Selected First View Controller structure.  The Second View Controller doesn't have that, because it gets loaded from an external .NIB.

Now, for "SecondView.xib".  It's File Owner's class is UIViewController.  It has an outlet defined, and it is associated to the View in the file.  All of this was done for us already. 

SecondView doesn't have a set of associated code files.  Well, I really like to do things in code because it usually ends up being more flexible later on, plus I find it helps me figure out WHY things work the way they do.

No comments:

Post a Comment