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

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.

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.

iPhone - placing tableview within a view in another nib. Window-based

I created a window based application, and I created a separate UITableViewController file called "HomeViewController" which right now only has a basic table.
In the MainWindow.xib file, I put a UIView in the bottom half of the screen, and I wish to put the HomeViewController tableview within this newly added UIView called "conferences".
Any suggestions as to how to push this file?
First off, usually your primary first view originates from a view controller that is loaded by the UIApplication object. The MainWindow nib's owner is UIApplication so you probably don't want to be mucking with the MainWindow nib. Rather, you want to muck with the view of the view controller loaded by MainWindow nib. If you look at the view displayed in IB for MainWindow.nib, it should say which view controller's view it is loading.
So, in IB for the view of view controller being loaded by MainWindow nib, this is where you want to place your UITableView. For purposes of this explanation, I will call this view controller, MucksViewController and associated nib, MucksView.nib.
I think what I would do, then, is drag and drop a UITableView into the view for MucksView.nib. Position it in the bottom half of the screen, as you described. Attach this UITableView to an IBOutlet property in MucksViewController header file. Next, drag and drop a UIViewController object into the main window for MucksView.nib. Make this UIViewController object's owner your HomeViewController class and also attach it to an IBOutlet property of type HomeViewController in MucksViewController's header file.
Now, in MucksViewController's class file, probably in viewDidLoad method, programmatically make the HomeViewController object the data source and delegate of the UITableView object.
But, I'm wondering, do you really need HomeViewController? It would be cleaner just to make MucksViewController the data source and delegate.
I hope this helps and is not too confusing.
Instead of a UITableViewController, use a UIViewController which implements the tableview delegate and datasource. in your MainWindow.xib, add a standard uitableview as a subview to the view where it should be. then also drag a HomeViewController to the xib (which should now be a uiviewcontroller sub class). click on the tableview, open the inspecor, go to connections, and drag the delegate and datasource to the HomeViewController in the xib.

viewDidLoad not called for UITableView loaded from XIB file

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...?

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.

Design view in Interface Builder to load lazily in UIViewController

I've managed to get my myself confused... I've got a fairly complex View Controller much of which is developed programatically. I'd like to pop up a "dialog" on top of the view controller at some point and I'd like to "design" that view in Interface Builder because it's fairly straightforward (background UIImageView, some UILabels and a UIButton).
I've created the .xib and am now subclassing UIView with the IBOutlets,etc. I'm now trying to wire it up and realizing I probably need to add an initWithNibName: method so this will instantiate correctly...then I realize that I'm really just making another UIViewController and I don't think we're supposed to have UIViewController views w/in other UIViewController views?!?
So now I'm re-thinking how to go about this "correctly." How best to use IB to design a simple 1/4 screen view that will pop up over a main view?
Call +[NSBundle loadNibNamed:owner:] to load the NIB file that contains your view. If you specify your view controller (i.e., self) as the owner, any connections you make to File's Owner in the NIB file will then be made to the view controller. So you could declare an outlet to your custom view in the view controller and after the call
[NSBundle loadNibNamed:#"MyView" owner:self];
the outlet variable will point to the view object. Alternatively, you can use -[NSBundle loadNibNamed:owner:options:], which returns an array of the top-level objects in the NIB.