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;
}
I feel I am close but I think I'm doing too much.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With