iOS Can I put all delegate method on another class

2019-05-21 18:58发布

问题:

What I want is to centralize all my delegate methods into a class. Where I can either use its defaults or override the delegate method.

For example:

ViewController.m

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@end

ViewController.h

#import "ViewController.h"
#import "TableDelegateContainers.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

TableDelegateContainers.h

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return sample.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cell;
}

Basically all the delegate methods that the UITableViewDataSource need will be put to another class. And when I import that class to thew ViewController.h those methods will be used. And I can either overwrite them if I want to.

Is this achievable by class? Or other entity is needed?

回答1:

I think he wants to extract the delegate methods into a separate class. so create a custom class:

TableViewHelper.h

@interface TableViewHelper : NSObject<UITableViewDataSource,UITableViewDelegate>
- (id) initWithTableView: (UITableView *) tableView;
@end

TableViewHelper.m

@implementation TableViewHelper

- (id) initWithTableView: (UITableView *) tableView {
    self = [super init];
    if (self) {
        tableView.datasource = self;
        tableView.delegate = self;
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return sample.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cell;
}
@end

ViewController.m

@interface ViewController ()
@property (nonatomic, strong) TableViewHelper *tvHelper;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tvHelper = [[TableViewHelper alloc] initWithTableView: self.tableView];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

so your ViewController does not need to implement both protocols anymore.



回答2:

For the documentation:

When a class adopts a protocol, it must implement the required methods the protocol declares, as mentioned earlier. In addition, it must conform to any protocols the adopted protocol incorporates. If an incorporated protocol incorporates still other protocols, the class must also conform to them. A class can conform to an incorporated protocol using either of these techniques:
- Implementing the methods the protocol declares
- Inheriting from a class that adopts the protocol and implements the methods

So basically you can create a class (a ViewController class for example) that conforms to the protocols, and you can make a specific InheritedViewController inherit from the other class that implements the protocols



回答3:

Create a CustomViewController (A UIViewController Subclass). Your custom view controller should confirm to all the protocols, provide datasource and handle delegates. After that when ever you create any UIViewController class, just inherit from your own CustomViewController, and override all the methods you need.

You may also keep an IBOutlet of UITableView, and connect with your child controllers where its needed. This way you will have many other benefits like you can keep activity indicator methods in common place, and handle other local notifications for common use.



回答4:

Use protocol for this

here i have added an user friendly link hope you get something relevant to you



回答5:

Yes, you can have it like this.

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    ViewController *vc = [[ViewController alloc] init];
    myTableView.delegate = vc; //Initialized Controller
    myTableView.dataSource = vc;
    // Do any additional setup after loading the view, typically from a nib.
}

@end

You can also do the same in the Xib too.