Xcode 4 and Assigning Tab Bar Item View Controllers

I ran into this last night- I used the sample from Xcode 4, “tab bar application.” I wanted to solve this problem I was having- I was assigning a UITableViewController to one of my Tab Bar Items, but I got a crash each time, with “retavalue” and “numberOfSections.” No matter how I set it up, it continued to crash. So I decided to create a new project with just that functionality and debug it. The problem is a lingering assignment in the new IB that’s part of Xcode 4.

First, create the project, “tab bar application”


Compile and run to test it without any modifications.

Next, create a new file that is a UIViewController, “UITableViewController” class:


We’re going to re-assign this to the second view on the Tab Bar. Edit some fields first-

<br /> - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView<br /> {<br /> return 1;<br /> }</p> <p>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section<br /> {<br /> return 3;<br /> }<br /> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath<br /> {<br /> static NSString *CellIdentifier = @"Cell";</p> <p> UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];<br /> if (cell == nil) {<br /> cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];<br /> }<br /> cell.textLabel.text = @"anna"; // same value repeated 3 times</p> <p> return cell;<br /> }</p> <p>

Now we have a view all set, let’s assign it to the second tab bar item, so when the user selects that, they see a table of “anna” listed 3 times.

1) Select MainWindow.xib on left side of Xcode.
2) Select the “second” tab bar item in the view. At the top you should see the “breadcrumps” and end in “second view controller”

Uploaded with Skitch!

3. In the inspector on the right, in the “Attributes Inspector” you can see the assignment to the “SecondView” Nib name. Now, for our tableview we don’t need a NIB. We’re going to delete this. This is where the assignment lingers.

4. After deleting that, go to the next inspector tab over, “Identity Inspector” and change the “custom class” to your new UITableViewController name. Mine is “testtableview”.

Uploaded with Skitch!

5. If you go back to “Attributes Inspector” it will still be there. Delete it again, and then click on another IB object such as the view. Click back to the Second Bar Tab Item and it will be gone. I found this by sheer experimentation. Compile and test.