UITableView datasource and delegate will not connect to user class

I cannot connect the data source and delegate the output of the table view in the storyboard to my custom delegated class. I would like to delegate these table functions to another class. There is something that I fundamentally misunderstood regarding delegation, exits, and posting things in a storyboard.

Background

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

I created the following class containing table methods such as numberOfSectionsInTableView:

 @interface ExerciseTableDelegate : NSObject <UITableViewDelegate, UITableViewDataSource> @property (strong, nonatomic) ExerciseDataController *dataController; @end 

I thought to put a link 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 in the storyboard, when I drag one of the data sources or delegates of my table view onto the UITableViewController , it will give me the ability to connect to my delegated class. This is not true.

Then I tried to create an object in the storyboard by providing it with the ExerciseTableDelegate class. Then I could drag the table view delegate into the object, but this is not the object that I configure in my AppDelegate .

My application 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; } 
  • Should I make my object single and still initialize it to a delegate?
  • Do I need to make this setting in code, and not in the Storyboard?
  • Does the object in the storyboard create the wrong idea?

I feel like I'm close, but I think I'm doing too much.

+6
source share
1 answer

If you need access to the ExerciseTableDelegate instance that you configured in the applicationโ€™s deletion, then you will have to connect it to the table view in the code, since it will not be accessible from the storyboard - since you Found, adding a new object to the storyboard creates a new instance.

Fortunately, this is pretty easy 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 reassigns the data source and delegates it to a separate object.

+1
source

Source: https://habr.com/ru/post/918830/


All Articles