viewDidLoad not called for UITableView loaded from XIB file - iphone

I have a XIB file in which I have a few UI elements. The resulting view is controlled by my own class derived from UIViewController. This one works fine, and viewDidLoad is called, when the XIB file gets loaded etc from the main application delegate.
However, in that XIB file I also have (obviously) some sub views. One of them is a UITableView. That one is being controlled by a custom class of mine derived from UITableViewController. I've set the delegate, datasource outlets on the one side, the UITableViewController also has got the correct view property set. However, the viewDidLoad of the UITableViewController is NOT called when the surrounding view loads.
I think this because the main UIView / UIViewController pair does not really know about the subview. in the UIViewController viewDidLoad I can call the subview's viewDidLoad. However I am suspecting that this is not the intended usage. So what should I do instead?
What I was assuming was, that subviews loaded from a xib file all get the viewDidLoad message.

You're correct in that you shouldn't call the subview's viewDidLoad: method, as it should always get called when the view is loaded (regardless of how the view is created). From the documentation:
This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.
This suggests to me that something (connections?) haven't been set up correctly in your .xib file, or that the controllers haven't been properly linked with their views. When you say that the UITableViewController has the correct view set, are you using the "view" property instead of the "tableView" property of the UITableViewController...?

Related

Can I bind xib file to view controller via IBOutlet?

I have a ViewController, and I want to dynamically load different UIView based on UISegmentedControl.I designed 3 UIViews in xib file. And I have set their File's Owner's Custtom class to my view controller name in Identify Inspector.After I connect my xib to view controller code via IBOutlet, I add the connected view via "addSubview(view)" method in viewDidLoad method. But When I run it, the compiler tells me that the connected view is null.
Instead of making three different xib files. Make one xib file with all three views in it, plus a fourth "default" view. Each view should be connected to a different IBOutlet in your class.
Then in your viewDidLoad figure out which view you want to display and addSubview it to your default view.

Best place to add a navigation bar button?

Where should I place my code that creates a UIBarButtonItem and assigns it to self.navigationItem.rightBarButtonItem? In initWithNibName or viewDidLoad?
EDIT: I should clarify that I'm loading a NIB from Interface Builder.
viewDidLoad is the correct place to create the button programatically whether you are loading a NIB from Interface Builder or not.
From the UIViewController class, viewDidLoad method documentation
This method is called after the view controller has loaded its
associated views into memory. This method is called regardless of
whether the views were stored in a nib file or created
programmatically in the loadView method. This method is most commonly
used to perform additional initialization steps on views that are
loaded from nib files.
In - (void) loadView if you do it pragmatically. Otherwise do as Rog suggests.

iPhone: how to create two controller in one NIB file?

I'm a little bit lost. I have created a small app that is starting with a tab bar and in one of its view there's a button that should open a Navigation view that contains a table view.
In my NIB file I have put a Navigation Controller that contains a TableView Controller. I have created a sub-class called MyTableViewController which inherits from the UITableViewController. In the NIB I have configured the Custom Class of the TableViewController with my subclass MyTableViewController.
When the button of my App is tapped, I'm loading the NIB file with the initWithNibName but it returns me a UINavigationController.
How does it work to request the creation of MyTableViewController and get a pointer on it when I'm loading my NIB?
Thanks,
Sébastien.
This one has caught me out a few times.
When you do initWithNibName it will take the class from the custom class of the File's Owner, not the custom view of any objects .
I dont actually bother subclassing from UITableviewController any more. Just create a view controller and drag in a table view as a subview. Just make sure you hook up the data source and the delegate.
Link your TableViewController to an IBOutlet so you don't have to mess with initWithNibName.

How do I reload a tableView contained within a UIViewController?

I want to reload the data within a table view that is contained on the root view of my application.
The root view of my application (called RootViewController) is a UIViewController that has a corresponding NIB file. I added a UITableView component to the RootViewController.xib via IB. In the header file RootViewController.h I ensure RootViewController conforms to . I have all this linked up correctly in IB and all the appropriate methods get called, viewWillAppear etc.
My question is, how can I get access to the UITableView so that I can call reloadData on it?
The following code won't work because 'self' is a UIViewController, not a UITableViewController.
[[self tableView] reloadData];
If I use the above code I get the following warning and my application crashes on startup:
'RootViewController' may not respond to '-tableView'
You need to add an IBOutlet to your controller and link the that outlet to the tableview within Interface Builder. The NIB loading process will reconnect these outlets, giving your controller a reference to the table view.

what to call viewDidLoad or loadview

I have a view controller class which loads from a .nib file. However, I also want to add controls (like UISwitch) to that view programmatically (UISwitch is not added to the nib file). In which portion of my code should I allocate the UISwitch control, viewDidLoad or loadView method?
I would do it on viewDidLoad. Definitely.
From Apple's documentation:
Discussion
This method is only invoked
when the view property is nil and it
is needed for display. You should not
invoke this method directly.
If you create the view that this view
controller manages programmatically,
then you should override this method
to create your view. The default
implementation creates a UIView object
with no subviews.
However, if you initialize the view
using a nib file—that is, you set
thenibName and nibBundle
properties—then you should not
override this method because the
default implementation already reloads
the nib file. Instead override the
viewDidLoad method to set any
properties after the nib file is
loaded.
In your case, the UIView is being created from the NIB file.
Use viewDidLoad. Additionally you should remove everything you added in the viewDidUnload method.
If you are loading from a NIB, implementing loadView will cause an error. Use viewDidLoad. As Pablo says, this is well documented by Apple.