代表与可可触摸事件
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I'm writing my first iPhone app, and I've been exploring the design patterns in Cocoa Touch and Objective-C. I come from a background of client-side web development, so I'm trying to wrap my head around delegates.

Specifically, I don't see why delegate objects are needed instead of event handlers. For instance, when the user presses a button, it is handled with an event (UITouchUpInside), but when the user finishes inputting to a text box and closes it with the 'Done' button, the action is handled by calling a method on the text box's delegate (textFieldShouldReturn).

Why use a delegate method instead of an event? I also notice this in the view controller with the viewDidLoad method. Why not just use events?

#0

EDIT: Another good post: NSNotificationCenter vs delegation( using protocols )?

A delegate is a callback so there is a 1:1 relationship. The delegate is a single instance of an object that implements a formal protocol.

Notifications (events) are basically broadcasts to many objects that are interested in when something happens.

Delegates are good for being able to interject code into the pipeline of some other objects processing such as before and after callbacks, providing data sources for a control and communicating between views:

What exactly does delegate do in xcode ios project?

Therefore delegates have a much tighter relationship with the object since they are the single provided object to interject and alter processing of the object or provide data. You're deferring decisions and external operations like loading data to some other object - which is why it's a very common pattern for generic UIKit classes. Notifications to other objects is a much looser relationship - it just that a notifies others that something happened.

It's also not a "vs" question necessarily. For example you could have an app that does background processing and it fired a something changed notification causing a view to call its data source delegate to refresh its view. They are two different mechanisms.

#1

Events and delegates have two different purposes, so you'll see both used.

If you just want your button press to fire off a message, an event is fine. If you want your Done button press to validate the context of the text field before it is allowed to lose focus, you can use the textFieldShouldReturn delegate method to handle any validation and return NO if it doesn't validate.

Delegates really allow you to change behaviour without subclassing. They're filled with should and did methods for this purpose. You implement these methods instead of overriding action methods when you want to validate, notify, or process before and/or after the action.

If you find yourself thinking that you need to subclass a UIKit object, check its delegate methods first. Chances are there's already a place to put your custom behaviour.

#2

One clear distinction is that delegate methods can have return values since there is a one-to-one relationship. On the other hand events are loosely coupled to the sending class, which usually doesn't care if anything responds or not.

Other delegate methods are simply there for convenience and can have corresponding events that are also triggered.

推荐文章

狗的电影收藏

狗的电影收藏

推荐文章

lua单元测试

lua单元测试

推荐文章

搭建本地blast2go

搭建本地blast2go

推荐文章

左倾堆C++实现

左倾堆C++实现

推荐文章

Lua正则表达式语言元素

Lua正则表达式语言元素

推荐文章

showModalDialog 页面跳转

showModalDialog 页面跳转

推荐文章

改造Lua的debug.traceback(),让其显示栈上所有的局部变量。

改造Lua的debug.traceback(),让其显示栈上所有的局部变量。

推荐文章

bst to dll

bst to dll

推荐文章

很有教养的英语口语

很有教养的英语口语

推荐文章

lua Table的序列化与反序列化函数

lua Table的序列化与反序列化函数

推荐文章

内部类的使用

内部类的使用

推荐文章

linux 软硬连接区别

linux 软硬连接区别

推荐文章

lua与c++数据交换之表和数组

lua与c++数据交换之表和数组

推荐文章

spark基础知识介绍

spark基础知识介绍

推荐文章

天龙 - Action Buffer

天龙 - Action Buffer

推荐文章

s3c2440的SD/MMC的应用

s3c2440的SD/MMC的应用