views:

149

answers:

2

Hi everyone,

I am working on my first iPhone app and making good progress. But there is one thing I just don't understand.

When my app starts it displays a UIView with some functionality on it. This works fine. One of the buttons on that screen is supposed to load a new view (HistoryViewController) which contains a navigation controller.

The problem is… whenever HistoryViewController is loaded the app crashes because there is no view. It's true because in the xib-File I can't connect the File's Owner's view to anything: http://www.freeimagehosting.net/image.php?1a3caa8b8d.png

I definitely have a lack of knowledge somewhere but after hours of research I have not been able to solve this problem.

Any hints? Thank you!

A: 

Normally you would either:

  • click on that bottom line (History Table View Controller, "HTVC") and in the inspector window specify a NIB Name - which means you would first have to make a new NIB.

or

  • doubleclick that bottom line (HTVC), so the 320x480 preview window pops up, and then drag in a UIView from the library.

Using the first method, you tell the view controller to dynamically load the NIB as the view to connect, and using the second method you do this for the view controller using IB. The view you drag in will then show up as a child of that bottom line (HTVC).

edit to actually load the nib file you created, do this to push the view controller:

    UIViewController *controller = [[UIViewController alloc] autorelease];
    [controller initWithNibName:@"nibfilename" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];

substituting UIViewController for your own view controller class (if needed) and nibfilename with the filename of the nib (minus the extension!)

mvds
Good tip, thanks! But I'm not there yet. Maybe you can help again?What have defined in IB:http://www.freeimagehosting.net/image.php?89f98c9824.pngWhat I get in iPhone:http://www.freeimagehosting.net/uploads/4721d096b0.pngObviously not the same as the navigation bar items are not being displayed and neither is the content of HistoryTableViewController (which works fine in a normal navigation-based app).Every little help appreciated! :)Thanks!
sprain
you kind of took a shortcut - you must do the things i said on the *bottom* line, the History Table View Controller. Thought I mentioned it 3 times?!
mvds
Hm… I think I did it all (but posted the wrong image before). This is in IB: http://www.freeimagehosting.net/uploads/3a620c6238.png but I still get the empty screen: http://www.freeimagehosting.net/uploads/4721d096b0.pngAppreciate your help. I'm really stuck here…
sprain
Thanks for the update. Now nothing at all is happening when I click the button, not even an entry on the console.Anyways, thanks a lot for your patience and help. I'll go get some sleep now and hope it'll work out better tomorrow :) - 1.30am over here in Europe.
sprain
I know, I'm there too ;-)
mvds
@mdvs - Your answer doesn't explain anything, but you're not explaining why he needs to do it a certain way. That's way I think the code route is better when you start - you understand how things work, and then you can make sense of IB.
Ron Srebro
mvds
A: 

It's hard to tell what your problem is exactly, but I'll offer some advice.

When creating a navigation controller (or tab controller for that matter) in interface builder, its easy to not understand what is really happening, so my suggestion drop interface builder for a second and lets build it in code.

In general I really dislike building either UI Navigation Controller or tab view controller in interface builder, I really just rather build the views themselves and create the UINavigationController in code.

You have a view which shows the HistoryTableViewController which you want to be contained in a UINavigationController so the code to do this is:

- (void) showHistory 
{
      HistoryViewController *historyVC = [[HistoryViewController alloc] init];
      // If you create historyviewcontroller in nib
      // HistoryViewController *histroyVC [[HistoryViewController alloc] initWithNibName:@"myNib" bundle:nil];

      UINavigationController *navController = [[UINavigationController alloc] initWithRootController:histroyViewController];
      [self presentModalViewContoller:navController animated:YES];
}

This will create a nav controller showing your history view controller as the root view controller. Can't be easier.

Ron Srebro
Thanks for your quick reply!Hm… this is exactly the code I have. Now there is happening what I also commented to the answer before: What have defined in IB: http://freeimagehosting.net/image.php?89f98c9824.png // What I get in iPhone: http://freeimagehosting.net/uploads/4721d096b0.png
sprain
IB is a perfect tool for getting things right quickly. Unneeded programmatical creation of things is probably not optimized and prone to other problems. Such as memory leaking, what your code sample does. **every `alloc` or `copy` should be followed by a `release` or `autorelease`**
mvds
@Manuel: see my updated answer on how to load the nib file you have created.
mvds
@mvds Yep, I know it's leaking. I'll take care of that later on.
sprain
IB is perfect when you understand how things work. It's hard to understand it when you are only building things using IB. And yeah, my example leaks, but it's not that it's going to be used, its just an example, to understand how UINavigationController work.
Ron Srebro