How to set dataSource for an outlet in a different view

Discussion in 'iOS Programming' started by danielkbx, Jun 8, 2009.

  1. danielkbx macrumors newbie

    Jun 8, 2009

    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
  2. drivefast macrumors regular

    Mar 13, 2008
    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.
  3. danielkbx thread starter macrumors newbie

    Jun 8, 2009
    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.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  6. justfred macrumors newbie

    May 8, 2009
    My understanding is that there are several ways to do this

    1. Through the app delegate
    2. By storing the data and reading a file or database
    3. By sending a notification, optionally with some user information contained in the notification object
    4. 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.

Share This Page