PDA

View Full Version : [iPhone SDK] Interface Builder + View Controllers




aoberoi
Aug 5, 2008, 04:53 PM
This is going to sound like a newbie question. My programming background is strong but I am very unfamiliar with the managed memory model that Objective-C 2.0 uses with the iPhone. That is where most of my troubles stem from.

I started reading the section on Navigation Bars in the Interface Builder User Guide (page 61) and I want to do it the way they suggest since it says it will take care of the memory issues of loading and unloading view objects the way its meant to be done.

Each navigation level in a navigation interface has its own view controller and set of views for
displaying the information at that level. The nib file containing the navigation controller object also
contains the root view controller for hierarchy. There are two ways to configure the views associated
with the root view controller:
■ Use a separate nib file to specify the views. (Recommended)
■ Embed the views in the same nib file that contains the root view controller.

I wanted to take the recommended path, so it further gives instructions for doing so.

To create the view hierarchy for the root view controller using a separate nib file, do the following:
1. Create a new nib file and configure it as described in “Using a View Controller as File’s Owner
of a Nib File” (page 55).
The class of the nib’s File’s Owner should be the same as your root view controller’s class.
2. In the nib file containing your navigation controller, select the root view controller embedded
inside the navigation controller object.
3. Open the Attributes inspector.
4. Set the class of the root view controller to your custom class.
5. In the Nib Name field of the inspector, enter the name of the nib file (without any filename
extension) you just created.
6. Save your nib file.
Configuring the Nib Name property of your view controller object in this manner is similar to creating
your view controller programmatically using the initWithNibName:bundle: method. Whenever
your application code requests its associated view, the view controller loads the specified nib file if
it is not already in memory. This gives the view controller the option of releasing the contents of the
nib file when they are not in use and the amount of free memory is running low.


What I ended up with was 2 nib files:

MainWindow.xib:

File's Owner is the UIApplication.
The AppDelegate has an outlet called navigationController that is connected to the UINavigationController object below.
There is UINavigationController with and its children are a UINavigationBar and my custom view controller class, BuildingsTableViewController. It also has a child (UINavigationItem). The attribute inspector lists "BuildingsTableView" as NIB name.


BuildingsTableView.xib:

File's Owner is BuildingsTableViewController.
At the top level there is also a UITableView object.
The File's Owner's view outlet is connected to the UITableView object.


My XCode project has a BuildingsTableViewController class which subclasses UIViewController but has no additional code other than the template code given when you make a new UIViewController subclass in XCode. The applicationDidFinishLaunching:application implementation looks like this:

[window addSubview:[navigationController view]];
// Override point for customization after app launch
[window makeKeyAndVisible];

I get a build error at launch, it looks like this:


Undefined symbols:
"_CGRectZero", referenced from:
_CGRectZero$non_lazy_ptr in BuildingsTableViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
"_CGRectZero", referenced from:
_CGRectZero$non_lazy_ptr in BuildingsTableViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Can anyone help me figure out what I am missing? Intuitively, I suspect that I have to instantiate a BuildingTableViewController programmatically somewhere, but I do not know where, and using which method. I am scared of this whole memory managment thing, so a response with detail in that area would be awesome.

THanks!

EDIT:

So it was a silly mistake, I was not linking to the CoreGraphics framework in my Xcode project. Oops.

But there still is a problem. I want to set the BuildingsTableViewController's UINavigationItem inside the BuilidingsTableView.xib. So I attempted to drag and drop one of these into the Document window using the Library at the root level. I then connected File's Owner's navigationItem outlet to this new instance and began setting the name up. Unfortunately, when I run, I still got the UINavigationItem that was set in the MainWindow.xib. Then I tried to remove the UINavigationItem from there, but I couldn't unless I moved the entire BuildingsTableViewController outside of the UINavigationController and in the root level of the Document window. At this point I chose to programatically add the BuildingsTableViewController to the navigationController inside the AppDelegate (using the pushViewController: animate: message). Then, keeping the UINavigationItem the way it was in BuilidingsTableView.xib I tried to run the program. This time i got a blank navigation bar. HOW DO I GET THIS RIGHT?