“代表”是什么意思在iPhone应用程序 [英] what do mean by "delegate" in iPhone application

查看:183
本文介绍了“代表”是什么意思在iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在苹果文档和他们的图书馆书中看到一个单词'delegate'。这个词有什么意思?在iPhone中有什么特别的意义?



非常感谢。

解决方案

代表是面向对象语言中的设计模式,允许对象调出未知代码来执行该对象无法自行执行的活动或计算。假设你有一个班狗:

  @protocol DogDelegate; 

@interface狗:狼<驯化>
- (void)树皮;
- (void)tiltHead Adorably;

- (void)playWithToy:(Toy *)aToy;
@property(readonly)玩具* favoriteChewToy;

@property(readwrite,assign)id&DogDelegate>代表; //DELEGATEPROPERTY DECLARED HERE
@end

委托对象通常由代码实例化 Dog ,并被该实例调用来执行狗本身不能做的事情。例如,考虑 DogDelegate 协议的这个接口,它定义了 Dog 的委托对象预期做什么:

  @protocol DogDelegate< NSObject> 
@required - (void)letDogOut:(Dog *)aDog;
@required - (void)letDogIn:(Dog *)aDog;
@optional - (void)scratchDog:(Dog *)aDog forTimeInterval:(NSTimeInterval)duration;
@end

在这种情况下, DogDelegate 通常是 Dog 的所有者(并且在Objective-C中,代理人通常拥有一个对象,所以这个比喻与隐喻相当。 )狗,当需要出去...狗活动...将要求其代表执行 -letDogOut:方法,将狗放在后院。完成后,将要求其代表执行 -letDogIn:方法,将狗带回内部。当狗想要感情时,如果代理人能够使用,那么代理将要求代理人刮一段时间。


I usually read a word 'delegate' in the apple document and their library book. What exactly meaning about this word? Any special meaning in iPhone?

Thank you very much.

解决方案

Delegates are a design pattern in object-oriented languages that allow an object to "call out" to unknown code to perform activities or calculations that that object cannot effectively do on its own. Let's say you have a class Dog:

@protocol DogDelegate;

@interface Dog : Wolf <Domesticated>
- (void)bark;
- (void)tiltHeadAdorably;

- (void)playWithToy: (Toy *)aToy;
@property (readonly) Toy *favoriteChewToy;

@property (readwrite, assign) id <DogDelegate> delegate; // "DELEGATE" PROPERTY DECLARED HERE
@end

The delegate object is generally supplied by the code that instantiates Dog, and is called upon by that instance to do things that the dog itself can't do. For instance, consider this interface of the DogDelegate protocol, which defines what the Dog's delegate object is expected to do:

@protocol DogDelegate <NSObject>
@required - (void)letDogOut: (Dog *)aDog;
@required - (void)letDogIn: (Dog *)aDog;
@optional - (void)scratchDog: (Dog *)aDog forTimeInterval: (NSTimeInterval)duration;
@end

In this case, an instance of DogDelegate is often the owner of the Dog (and, in Objective-C, a delegate often owns an object, so this lines up nicely with the metaphor.) The dog, when it needs to go out for... dog activities... will ask its delegate to perform the -letDogOut: method, placing the dog in the backyard. When done, it will ask its delegate to perform the -letDogIn: method, bringing the dog back inside. When the dog wants affection, if its delegate is able to, it will ask the delegate to scratch it for some period of time using -scratchDog:forTimeInterval:.

这篇关于“代表”是什么意思在iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆