代表申报困境

发布于 2024-10-06 08:41:20 字数 409 浏览 3 评论 0原文

我很困惑 - 我无法理解代表的用途是什么?

默认创建的应用程序委托是可以理解的,但在某些情况下我见过这样的东西:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-10-13 08:41:20

表示该类符合 UIScrollViewDelegate 协议

这真正意味着该类必须实现 UIScrollViewDelegate 协议中定义的所有必需方法。就这么简单。

如果您愿意,您可以使您的类符合多个协议:

@implementation MyClass : UIViewController <SomeProtocol, SomeOtherProtocol>

使类符合协议的目的是 a) 将类型声明为协议的一致者,因此您现在可以在 id下对此类型进行分类;,这对于此类的对象可能属于的委托对象来说更好,并且 b) 它告诉编译器不要警告您实现的方法未在头文件中声明,因为您的类符合协议。

下面是一个示例:

Printable.h

@protocol Printable

 - (void) print:(Printer *) printer;

@end

Document.h

#import "Printable.h"
@interface Document : NSObject <Printable> {
   //ivars omitted for brevity, there are sure to be many of these :)
}
@end

Document.m

@implementation Document

   //probably tons of code here..

#pragma mark Printable methods

   - (void) print: (Printer *) printer {
       //do awesome print job stuff here...
   }

@end

您可以然后拥有多个符合 Printable 协议的对象,然后可以将其用作实例变量,比如说一个 PrintJob 对象:

@interface PrintJob : NSObject {
   id <Printable> target;
   Printer *printer;
}

@property (nonatomic, retain) id <Printable> target;

- (id) initWithPrinter:(Printer *) print;
- (void) start;

@end

@implementation PrintJob 

@synthesize target; 

- (id) initWithPrinter:(Printer *) print andTarget:(id<Printable>) targ {
   if((self = [super init])) {
      printer = print;
      self.target = targ;
   }
   return self;
}

- (void) start {
   [target print:printer]; //invoke print on the target, which we know conforms to Printable
}

- (void) dealloc {
   [target release];
   [super dealloc];
}
@end

<UIScrollViewDelegate> is saying that the class conforms to the UIScrollViewDelegate 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:

@implementation MyClass : UIViewController <SomeProtocol, SomeOtherProtocol>

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

@protocol Printable

 - (void) print:(Printer *) printer;

@end

Document.h

#import "Printable.h"
@interface Document : NSObject <Printable> {
   //ivars omitted for brevity, there are sure to be many of these :)
}
@end

Document.m

@implementation Document

   //probably tons of code here..

#pragma mark Printable methods

   - (void) print: (Printer *) printer {
       //do awesome print job stuff here...
   }

@end

You could then have multiple objects that conform to the Printable protocol, which could then be used as an instance variable in, say, a PrintJob object:

@interface PrintJob : NSObject {
   id <Printable> target;
   Printer *printer;
}

@property (nonatomic, retain) id <Printable> target;

- (id) initWithPrinter:(Printer *) print;
- (void) start;

@end

@implementation PrintJob 

@synthesize target; 

- (id) initWithPrinter:(Printer *) print andTarget:(id<Printable>) targ {
   if((self = [super init])) {
      printer = print;
      self.target = targ;
   }
   return self;
}

- (void) start {
   [target print:printer]; //invoke print on the target, which we know conforms to Printable
}

- (void) dealloc {
   [target release];
   [super dealloc];
}
@end
孤君无依 2024-10-13 08:41:20

我认为您需要了解委托模式。它是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文