I’m relatively new to Xcode.
I am trying to do this in Xcode’s Interface Builder as much as possible. Because I am working with legacy code, Storyboards are not an option.
I’m working with a XIB I’ll call DualTVController. It is based on a simple UIViewContoller.
In this XIB I have two UITableViews, the LeftTV and the RightTV. In IB, I am able to assign the delegate/datasource of the LeftTV to the File’s Owner, which is the class representation of the XIB. This works fine.
I want to assign the delegate/datasource of the RightTV in IB. So far, I’ve successfully been able to use the LeftTV for this, but that’s just confusing.
Instead, I have created a UITableViewController subclass called RightDelegate, and assigned it as the class of a simple Object I create through the IB. I made it follow the UITableViewDataSource and UITableViewDelegate protocols. This allows me to hook up the delegate/datasource of RightTV to it in IB, but when I try to run it, I get a BAD_ACCESS error.
I have the same delegate code in RightDelegate as I do in LeftTV (which works). Can anyone suggest the problem? Am I responsible for instantiating RightDelegate in code?
Thanks for any help.
note: I know you want to do this in IB, but in this case this is probably best handled in code. setting delegates for a single tableview in IB is simple, but eaisly overlooked. Since the delegates are so important, I usually keep them in code so there less mistakes.
when you are going to use more than one tableview there are a couple of ways to manage the delegates.
a) manage both delegates in your view controller with logic on each delegate callback to determine which tableview is asking (ends up messy)
b) split delegates by leaving one in your vc and putting the other in its own separate controller (somewhat confusing)
c) split delegates by using a separate tableview controller instance for each tableview. This is probably the best choice but it does require 3 controllers – your initial view controller that will init the xib file and then two separate tableview controllers – leftTable, rightTable.
in your main view controller, make sure you setup a property for each tableview:
then include your view controllers
then initialize the tableview controllers and set the delegates
Then just add code in each tableview controller to manage the tableviews.
If you need the tableview controller to call your main view controller then use delegate callbacks.
hope this helps, best of luck.