如何处理多个代表

如何处理多个代表

本文介绍了如何处理多个代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个视图,几乎都是一切,我喜欢这样。然而,问题是它正在执行5或6个不同的代理,这似乎有点混乱。

I've got a view in my app that does pretty much everything, and I like it that way. The problem however is that it's implementing 5 or 6 different delegates, which seems a little bit messy.

我的问题是,视图控制器是否必须实现所有的代理?或者有什么方法可以将代码分离成不同的文件(无需进行重大的重组或重写)?

My question is, does the view controller have to implement all of the delegates? or is there some way I can separate the code out into different files (without having to do a major restructure or rewrite)?

这是我正在实施的所有代理:

Here's all the delegates I'm implementing:

@interface MyView : UIViewController <UIScrollViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate>


推荐答案

没问题。解决方案是Objective-C类。您可以将其放在单独的源文件中:

No problem. The solution is Objective-C categories. You can put this in a separate source file:

#import "MyView.h"
@implementation MyView (UIScrollViewDelegate)
// scroll view delegate method implementations go here
@end

A很好的约定是将此文件命名为MyView + UIScrollViewDelegate.m。您在这里定义的方法将像在MyView.m中定义一样。

A nice convention is to name this file "MyView+UIScrollViewDelegate.m". The methods you define here will behave just as if they were defined in "MyView.m".

不需要类别的名称来匹配协议的名称。您可以使用任何方法来完成此操作,并使用您喜欢的任何类别名称。

There's no need for the name of the category to match the name of the protocol. You can do this with any set of methods and use any category name you like.

这篇关于如何处理多个代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:17