简体   繁体   English

行动方法和协议方法之间有什么区别?

[英]What’s the difference between action methods and protocol methods?

I started learning iOS programming, and I came across something like this: to handle the event when users press return after input text in a UITextField, my view controller implements "textFieldShouldReturn: textField" from UITexTFieldDelegate protocol. 我开始学习iOS编程,我遇到过这样的事情:当用户在UITextField中输入文本后按回车来处理事件时,我的视图控制器从UITexTFieldDelegate协议实现“textFieldShouldReturn:textField”。 But, to handle the event when "Editing Did End", I need to declare an IBAction method in the view controller, and wire up the event to it. 但是,要在“Editing Did End”中处理事件,我需要在视图控制器中声明一个IBAction方法,并将事件连接到它。

What is the difference between these two types of event handling methods? 这两种事件处理方法之间有什么区别?

The "Editing Did End" is part of the UIControl class, and UITextField inherits it from UIControl. “ Edited Did End”是UIControl类的一部分,UITextField继承自UIControl。 This kind of event is one of the registered touch events this class responds to and the way to do this registration is to assign an action (IBAction using IB) to the particular event. 此类事件是此类响应的已注册触摸事件之一,并且执行此注册的方式是为特定事件分配操作(使用IB的IBAction)。

The delegate protocol part of the implementation is due to the "extension mechanism" provided by delegate protocols in Cocoa design patterns. 实现的委托协议部分是由于Cocoa设计模式中的委托协议提供了“扩展机制”。 Essentially the idea is that you can provide special behavior to the object, in this case UITextField, without subclassing. 本质上,这种想法是,您可以为对象提供特殊的行为,在本例中为UITextField,而无需子类化。 Imagine for example you want some kind of validation before allowing the "return" button. 例如,假设您想在允许“返回”按钮之前进行某种形式的验证。 In such case you would be forced to setup a UITextField subclass with this validation code hard-coded in it. 在这种情况下,您将被迫设置一个UITextField子类,并在其中硬编码此验证代码。 And for each different validation code you would be forced to implement a different subclass. 对于每个不同的验证代码,您将不得不实施一个不同的子类。 Using the delegate mechanism, you delegates something else (typically the text field owner view controller) to perform this "class extension", without subclassing. 使用委托机制,您委派其他东西(通常是文本字段所有者视图控制器)来执行此“类扩展”,而不进行子类化。 So you do class customization per-instance. 因此,您可以按实例进行类自定义。

Note that this approach is coherent: everything that is considered as "event" (touch up, touch down, did end editing, all related to some user interaction with the display and not with the keyboard) is wired to the class using the UIControl target-action mechanism. 请注意,这种方法是连贯的:被视为“事件”的所有内容(触摸,触摸,结束编辑,所有与某些用户与显示器(而非键盘)交互有关的事件)都使用UIControl目标连接到类 - 动作机制。 Everything that is "specific" of the instance (and not the class!) is managed using the delegate. 使用委托管理实例(而不是类!)的“特定”的所有内容。

In protocols (denoted with @protocol //name\\ superclass) one can specify methods that the conforming class either can, will, or must take on with @optional, or @required methods. 在协议(用@protocol // name \\ superclass表示)中,可以指定符合类可以,将要或必须采用@optional或@required方法的方法。 One might use protocols to say, pass data from one view to the next, so long as the second view conformed to the protocol (like @implementation Classname: NSObject ) This is in stark contrast to IBActions and void functions which are local and impassable to other controllers (except with class methods). 有人可能会使用协议来说,将数据从一个视图传递到下一个视图,只要第二个视图符合协议(如@implementation Classname:NSObject)这与IBActions和void函数形成鲜明对比,这些函数是本地的,无法通过其他控制器(类方法除外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM