简体   繁体   English

iOS我可以将所有委托方法放在另一个类上。 将委托继承到视图控制器中

[英]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中执行相同的操作。

Use protocol for this 使用协议

here i have added an user friendly link hope you get something relevant to you 在这里,我添加了一个用户友好的链接,希望你能得到与你相关的东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个iOS访问一个控制器的表视图委托方法 - How can I access a table view delegate methods of one controller from another ios 如何从iOS中的另一个类调用视图控制器方法? - How do I call a view controller method from another class in iOS? 如何使用委托将消息从一个视图控制器传递到另一个使用委托的视图控制器? - How to pass message using delegates from one view controller to another using delegate? 如何向视图控制器添加多个委托? - How can i add multiple delegates to my view controller? 不能通过委托从另一个类执行方法? - Can not execute method from another class by delegate? 从 iOS 中的 App Delegate 调用当前视图 controller 中的方法 - Calling method in current view controller from App Delegate in iOS @obj协议委托方法在第二类中不起作用。 迅速 - @obj protocol delegate method not working in second class. Swift 有没有办法可以将我的所有视图控制器日期选择器逻辑放入一个单独的类中,以保持我的代码有条理? - Is there a way I can put all of my view controller date picker logic into a separate class to keep my code organized? 如何将另一个视图控制器的数组保存到应用程序委托中的文件中? - How can I save an array of another view controller to the file within the app delegate? 将委托传递给另一个视图控制器的委托 - Passing a delegate to another view controller's delegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM