Gero
Gero

Reputation: 2967

Subviews not loaded from NIB with initWithNibName

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.

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?

Upvotes: 2

Views: 14298

Answers (5)

Gary
Gary

Reputation:

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.

Upvotes: 0

Gero
Gero

Reputation: 2967

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.

Upvotes: 2

Francescu
Francescu

Reputation: 17054

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";

Upvotes: 1

Dimitris
Dimitris

Reputation: 13660

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.

Upvotes: 1

Ralf Edmund
Ralf Edmund

Reputation: 1035

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.

Upvotes: 0

Related Questions