tags:

views:

3709

answers:

5

I've got the code below in my RootViewController (which is a UITableViewController). This code is executed when the button in the navigation bar is clicked and then (in the simulator) the next view is shown. However, the subviews (UITextLabels, UIButtons) that are drawn in in the view managed by the TripDetailsController are not displayed. Also, in the navigation bar in the top of the screen, the 'back' button to the original view is not shown, but when I click on the left side of the navigation bar it does transition back to the original view.

  • TripDetailsController.view is linked to the TripDetailsView (UIView) in Interface Builder
  • tdController does have a value, so it looks as if it is loaded
  • TripDetailsController is in a separate NIB file
  • Using iPhone SDK 2.2.1 (so not 3.0 yet)

Code:

 TripDetailsController *tdController = [[TripDetailsController alloc]
                  initWithNibName:@"TripDetailsController" bundle:nil];
 [self.navigationController pushViewController:tdController animated:YES];
 [tdController release];

In the TripDetailsController class I added the viewDidLoad method like this:

Code:

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.title = @"Reis Details";    

  NSLog(@"Subviews: %@", self.view.subviews);
  UILabel *l = [self.view.subviews objectAtIndex:0];
  NSLog(@"Subview 0 bounds: %@", l.bounds);
}

The log messages below do show that the subviews are there, but also that the bounds are not set:

Code:

6/18/09 Jun 18, 2009   10:06:00 PM ReisAdvies[11226] Subviews: (
    <UILabel: 0x56f250>,
    <UILabel: 0x56f5a0>,
    <UILabel: 0x56f6b0>,
    <UILabel: 0x56f780>
) 
6/18/09 Jun 18, 2009   10:06:00 PM ReisAdvies[11226] Subview 0 bounds: (null)

In Interface Builder the "Label size" tab does show values for X/Y/W/H. Feels like I have to trigger it to do some layout activities, but call layoutSubviews in the viewDidLoad() does not help. Any ideas what might be the problem here?

A: 

The way you navigate to the subview looks generally fine for me. Only thing i would mention is your [TripDetailController -loadView] message. I never did something like this. Normall i simply transfer some state information to the dependent controller before adding it to the UINavigationController relying on the standard mechanisms (namely viewDidLoad) to initialize the controller's logic.

Do you receive a -viewDidLoad message? Is your controller up and running in that moment?

Probably it is a subtle wiring problem / type somewhere in your project.

Ralf Edmund
I added the loadView call because it was suggested as a possible solution. It does not have any effect here, so I'll remove it from the original question.Yes, the TripDetailsController dos receive the viewDidLoad message. So I'm assuming the TripDetailsController is up and running somewhere. I also have the feeling that it's a subtle wiring problem but have not been able to spot it yet...
Gero
+1  A: 

The first thing I notice in your code is that you call a function like:

tdController.loadView;

As far as I know, that can't be done in objective-c. You have to call it like:

[tdController loadView];

Also, where do you do all your initialiation? In the "loadView", in the "viewDidLoad"?

Try adding the controller to the screen with something like this (push Views instead of ViewControllers):

[self.view insertSubview:tdController.view atIndex:0];

The above code supposes that it is run inside another UIViewController subclass, that load your controller's view into it's own view as a subview.

Dimitris
I tried adding the view like you described but that did not have any (visible) effect.
Gero
+1  A: 

Also, in the navigation bar in the top of the screen, the 'back' button to the original view is not shown, but when I click on the left side of the navigation bar it does transition back to the original view.

Did you set a title for your navigationItem ?

For example, in TripDetailsController viewDidLoad

self.navigationitem.title = @"trip detail";
F.Santoni
When I do that, the title shows up. Rest of the controls does not show up.I'm assuming that because I layed out the view in INterface Builder I do not have to add the UITextLabels etc. in the code of the TripDetailsControllers viewDidLoad method anymore. Or do I have to code that there also? If so, what would be the value of designing the view in Interface Buildes?
Gero
+1  A: 

Found the solution, in Interface Builder:

  1. In the NIB file I did not include a UIWindow, so I added a UIWindow to the NIB file and connected the TripDetailsController.view outlet to the newly added UIWindow.
  2. The File's Owner view property is connected to the TripDetailsView (UIView)
  3. The File's Owner class is set to UIViewController

Now it works as expected.

Not 100% sure if this is 'the way' to do it, but at least it seems to work OK for me.

Gero
this is the way, but it should have been done automagically for you by xcode if you let it generate the xib for you
slf
A: 

Here's what worked for me...

Stripped out Window and ViewControl from the XIB and made the File's Owner my ViewCOntroller class, then connected file owner view to my view.

Did the load with initWithNibName...

New to iPhone Dev and a little bit frazzled by how many ways I can hang myself in IB.