代表申报困境
我很困惑 - 我无法理解代表的用途是什么?
默认创建的应用程序委托是可以理解的,但在某些情况下我见过这样的东西:
@interface MyClass : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
//...
@end
for? 是什么?
它是如何工作的以及为什么使用它?
I'm confused - I cannot understand what is the delegate is for?
The Application Delegate which is created by default is understandable, but in some cases I've seen something like this:
@interface MyClass : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
//...
@end
What is the <UIScrollViewDelegate>
for?
How does it work and why is it used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表示该类符合UIScrollViewDelegate
协议。这真正意味着该类必须实现 UIScrollViewDelegate 协议中定义的所有必需方法。就这么简单。
如果您愿意,您可以使您的类符合多个协议:
使类符合协议的目的是 a) 将类型声明为协议的一致者,因此您现在可以在
id下对此类型进行分类;
,这对于此类的对象可能属于的委托对象来说更好,并且 b) 它告诉编译器不要警告您实现的方法未在头文件中声明,因为您的类符合协议。下面是一个示例:
Printable.h
Document.h
Document.m
您可以然后拥有多个符合
Printable
协议的对象,然后可以将其用作实例变量,比如说一个PrintJob
对象:<UIScrollViewDelegate>
is saying that the class conforms to theUIScrollViewDelegate
protocol.What this really means is that the class must implement all of the required methods defined within the
UIScrollViewDelegate
protocol. Simple as that.You can conform your class to multiple protocols if you like:
The purpose of conforming a class to a protocol is to a) declare the type as a conformant of the protocol, so you can now categorize this type under
id <SomeProtocol>
, which is better for delegate objects that objects of this class may belong to, and b) It tells the compiler to not warn you that the implemented methods are not declared in the header file, because your class conforms to the protocol.Here's an example:
Printable.h
Document.h
Document.m
You could then have multiple objects that conform to the
Printable
protocol, which could then be used as an instance variable in, say, aPrintJob
object:我认为您需要了解委托模式。它是 iphone/ipad 应用程序使用的核心模式,如果您不理解它,您将无法走得更远。我刚刚使用的维基百科链接概述了该模式,并给出了它的使用示例,包括 Objective C。这将是一个很好的起点。另请查看 概述教程来自 Apple,该文档特定于 iPhone,并且还讨论了委托模式。
I think you need to understand the Delegate Pattern. It is a core pattern used by iphone/ipad applications and if you don't understand it you will not get far. The link to wikipedia I just used outlines the pattern and gives examples of it's use including Objective C. That would be a good place to get started. Also look at take a look at the Overview tutorial from Apple which is specific to the iPhone and also discusses the Delegate pattern.