View Full Version : How to set dataSource for an outlet in a different view
danielkbx
Jun 8, 2009, 10:48 PM
Hello,
I never had the situation to have TableViews in different views. It is quite easy to connect the data source for a table view to an object within Interface Builder. However, if you have the table view in a subview in a different xib file, you cannot drag the data source element to an object in a different xib file. How do I do this?
Thanks a lot, danielkbx
drivefast
Jun 9, 2009, 12:43 AM
if you want to do the assignments in code (as opposed to making them with the IB), start with the idea of
myTableView1.dataSource = myTableView2.dataSource;
better yet, create a class that will be a common data source for both:
myTableView1.dataSource = myDataSource;
myTableView2.dataSource = myDataSource;
where myDataSource is previously declared as an instance of your class. your class must implement the <UITableViewDataSource> protocol. to have access to myDataSource from both views (view controllers?) associated code, you may want to make it a property of a class that is accessible from both; a common way to place it is in the application delegate.
danielkbx
Jun 9, 2009, 01:17 AM
Thanks drivefast.
I know how to use a data source protocoll. I just need to know how to tell an UITableView which object (e.g. the appDelegate) is its datam source when the object is in a complete different xib file.
Normally, you just connect the object and the TableView's data source outlet. But when both are in different xib files I cannot connect them.
I'd say it is a quite common problem, since every TabBarView works this way. You've got all the data in a nice object and several views which use this objects. You won't create a new instance of this object for every view.
dejo
Jun 9, 2009, 09:47 AM
I'm still not sure I understand your problem. Can you explain more about your statement "how to tell an UITableView which object is its datasource when the object is in a complete different xib file". What kind of "object" are we talking about? Normally, the datasource for a UITableView is defined as a UITableViewController. Maybe you could give us an example of how your tableViews and XIBs are set up. My guess is you don't have the needed understanding of how view-controllers enter into the picture, because that is also what is normally used with a UITabBar as well, via the UITabBarController class.
PhoneyDeveloper
Jun 9, 2009, 06:34 PM
If you want to use the same data source for tables in different nibs probably the simplest way to do this is with code, not IB. That is create the table and whatever UI you have in IB but set the data source in the view controller's viewDidLoad.
There might be a way to instantiate a datasource object in one nib and have it visible to tables in other nibs but I don't know how.
You could also have your datasource be the respective view controllers and have their methods just call into the real shared datasource object.
justfred
Jun 10, 2009, 02:42 AM
My understanding is that there are several ways to do this
Through the app delegate
By storing the data and reading a file or database
By sending a notification, optionally with some user information contained in the notification object
Using an object to represent the data model, view controllers delegating to the same source, and code linking the whole thing together. It is called MVC or Model-View-Controller. Going through the app delegate is a shortcut to this design pattern.
There may be others. Some options are better for certain situations. I'm a newb, but I've been working on a very similar issue for a week or two. I did it with notifications.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.