[英]iOS Can I put all delegate method on another class. Inherit the delegates into a view controller
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 ViewController.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@end
ViewController.h 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 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. 基本上UITableViewDataSource需要的所有委托方法都将被放到另一个类中。 And when I import that class to thew ViewController.h those methods will be used. 当我将该类导入到ViewController.h时,将使用这些方法。 And I can either overwrite them if I want to. 如果我愿意,我可以覆盖它们。
Is this achievable by class? 这可以通过课堂实现吗? Or other entity is needed? 还是需要其他实体?
I think he wants to extract the delegate methods into a separate class. 我认为他想将委托方法提取到一个单独的类中。 so create a custom class: 所以创建一个自定义类:
TableViewHelper.h TableViewHelper.h
@interface TableViewHelper : NSObject<UITableViewDataSource,UITableViewDelegate>
- (id) initWithTableView: (UITableView *) tableView;
@end
TableViewHelper.m 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 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. 所以你的ViewController不再需要实现这两个协议了。
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 所以基本上你可以创建一个符合协议的类(例如一个ViewController
类),你可以使一个特定的InheritedViewController
继承自实现协议的另一个类。
Create a CustomViewController (A UIViewController Subclass). 创建一个CustomViewController(一个UIViewController子类)。 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. 之后,当您创建任何UIViewController类时,只需从您自己的CustomViewController继承,并覆盖您需要的所有方法。
You may also keep an IBOutlet of UITableView, and connect with your child controllers where its needed. 您还可以保留UITableView的IBOutlet,并在需要时与您的子控制器连接。 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. 通过这种方式,您可以获得许多其他好处,例如您可以将活动指示器方法保持在常见位置,并处理其他常用的本地通知。
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. 您也可以在Xib中执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.