在iphone sdk中使用Delegate [英] use of Delegate in iphone sdk

查看:219
本文介绍了在iphone sdk中使用Delegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下,代理人在iphone sdk中的工作原理.....
一个简单的例子,如何使用委托以及使用委托的优点。

解决方案

代理模式在iPhone SDK中广泛使用。请考虑以下示例:


  1. 您正在运行动画。底层系统为您处理动画。但是,当动画结束时,您想要做某些事情(比如说,想要激活按钮或在动画结束时显示一些文本)是很自然的事情。现在动画系统如何知道动画结束后该怎么办?毕竟这是你的自定义任务。因此,您将为动画配置代理,并且在动画结束时系统将调用该委托方法。显然,您将在此委托方法中执行自定义任务。


  2. 您有一个文本字段,您想知道用户何时在字段中点击或编辑了某些内容。你怎么知道的?您将为您的文本字段配置一个委托,并且当该特定字段被编辑或点击时,UITextField类将调用预定义的委托方法。


  3. 忘记UIApllicationDelegate?系统完成加载和运行应用程序的工作。它将如何告诉你,它的初始化已经完成,你现在可以运行你的代码?它将调用您的应用程序委托的applicationDidFinishLaunching方法。


  4. 您正在进行异步http请求。加载数据后,将调用委托方法,以便现在可以使用数据。


还有更多的例子。为了使用委托,您将需要指定委托对象,有时还需要指定选择器。需要做什么取决于你在做什么也就是说,配置动画委托与配置文本字段委托不同。但是一般的过程是一样的,那就是你需要指定你的委托对象。



动画示例代码:

 
CATransition * animation = [CATransition animation] ;
[animation setDelegate:delegate]; //这里委托是您的委托对象

动画结束后,您的委托对象的

  - (void)animationDidStop: (CAAnimation *)theAnimation finished:(BOOL)标志

将被调用,您将在此方法中进行自定义。


Can somebody explain me how exactly does the delegate work in iphone sdk.....??? A simple example how to use the delegate and what are the advantages of using delegate.

解决方案

Delegate pattern is used widely in iPhone SDK. Consider the examples:

  1. You are running an animation. The underlying system handles the animation for you. But it is natural that you want to do something when the animation is over (say, you want to activate a button or show some text when animation is over). Now how the animation system will know what to do when animation is over? After all this is your custom task. So you will configure a delegate for the animation and the system will call that delegate method when the animation is over. Obviously you will do your custom tasks in this delegate method.

  2. You have a text field and you want to know when the user have tapped or edited something in the field. How you will know that? You will configure a delegate for your text field and predefined delegate method will be called by the UITextField class when that particular field is edited or tapped.

  3. Forget UIApllicationDelegate? The system does the job of loading and running the app. How it will tell you that it's initialization have finished and you can now run your code? It will call applicationDidFinishLaunching method of your app delegate.

  4. You are making an asynchronous http request. After loading the data, your delegate method will be called so that you can now work with the data.

There are many more examples. In order to use delegate, you will require to specify the delegate object and sometimes the selector also. What exactly is needed to be done is dependent on what are you doing. That is, configuring an animation delegate is different from configuring a text field delegate. But the general procedure is same, that is you need to specify your delegate object.

Example code for animation :

CATransition *animation = [CATransition animation];
[animation setDelegate:delegate];  // here delegate is your delegate object

After animation is over, your delegate object's

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag

will be called and you will do your customization in this method.

这篇关于在iphone sdk中使用Delegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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