Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView datasource and delegate won't connect to a custom class

I am unable to connect the datasource and delegate outlets of table view within the storyboard to my custom delegated class. I would like to delegate those table functions to another class. There is something I've fundamentally misunderstood regarding delegation, outlets and wiring things up in a storyboard.

Background

I have a UIViewController that has a view containing a UIPickerView and amongst other things, a UITableView.
I have reached the point where my UIViewController is too large and I would like to move the table-related functions into another class.

I have created the following class to contain those table methods such as numberOfSectionsInTableView:.

@interface ExerciseTableDelegate : NSObject <UITableViewDelegate, UITableViewDataSource> 

@property (strong, nonatomic) ExerciseDataController *dataController;

@end

I've thought to put a reference to the class above in my UIViewController

@interface ExerciseViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
    UIPickerView *exercisePicker;
}

@property (strong, nonatomic) IBOutlet ExerciseTableDelegate *tableDelegate;

@end

I was hoping that within the storyboard, when I drag one of the datasource or delegate outlets of my table view onto the UITableViewController it would give me the ability to connect to my delegated class. It didn't.

I then tried to create an object within the storyboard, giving it the class ExerciseTableDelegate. I could then drag the table view delegate to the object, but this isn't the same object that I setup within my AppDelegate.

My app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    ExerciseViewController *rootViewController = (ExerciseViewController *)[[navigationController viewControllers] objectAtIndex:0];

    ExerciseTableDelegate *tableDelegate = [[ExerciseTableDelegate alloc]init];
    ExerciseDataController *dataController = [[ExerciseDataController alloc] init];

    tableDelegate.dataController = dataController;
    rootViewController.tableDelegate = tableDelegate;

    // Override point for customization after application launch.
    return YES;
}
  • Do I need to make my object a singleton and still initialise it in the delegate?
  • Do I need to do this setup in code rather than in the Storyboard?
  • Is creating an Object in the Storyboard the wrong idea?

I feel I am close but I think I'm doing too much.

like image 288
AllTheThingsICanDo Avatar asked Jun 23 '12 15:06

AllTheThingsICanDo


1 Answers

If you want access to the instance of ExerciseTableDelegate that you set up in your application delegate, then you will have to connect it to your table view in code, since it won't be accessible from the storyboard - as you've found, adding a new object in the storyboard creates a new instance.

Luckily, this is pretty simple to implement. In the viewDidLoad method of your table view controller, add the following:

self.tableView.delegate = self.tableDelegate;
self.tableView.datasource = self.tableDelegate;

This will re-point the datasource and delegate to your separate object.

like image 119
jrturton Avatar answered Nov 15 '22 04:11

jrturton