iOS 委托的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7052926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:05:00  来源:igfitidea点击:

What is the purpose of an iOS delegate?

ios

提问by Louis

I understand what a delegatedoes in iOS, and I've looked at sample code, but I'm just wondering about the advantages of this type of encapsulation (as opposed to including delegate methods in the primary object).

我了解委托在 iOS 中的作用,并且查看了示例代码,但我只是想知道这种类型的封装的优点(与在主要对象中包含委托方法相反)。

采纳答案by Patrick

Delegation is a design patternnot only used in iOS but many other languages. It enables you to hand values and messages over in your class hierarchy.

委托是一种不仅在 iOS 中而且在许多其他语言中使用的设计模式。它使您能够在类层次结构中传递值和消息。

回答by John Topley

The advantage of the delegate design pattern is loose coupling. It enables class A (the delegate) to depend on class B (the delegating class) without class B having to have any knowledge of class A. This ensures that the dependency relationship is one-way only, rather than being circular.

委托设计模式的优点是松耦合。它使 A 类(委托)能够依赖 B 类(委托类),而 B 类不必了解 A 类。这确保了依赖关系是单向的,而不是循环的。

It also forms the foundation (lower case "f") of Apple's frameworks because it allows them to invoke your code as appropriate when functionality specific to your application is required. For example, responding to a button tap or telling a table view how many sections there should be.

它还构成了 Apple 框架的基础(小写“f”),因为当需要特定于您的应用程序的功能时,它允许它们适当地调用您的代码。例如,响应按钮点击或告诉 table view 应该有多少个部分。

回答by mskw

In iOS, delegation requires the "delegate" class to implement a protocol which contain methods that the "delegating" knows about. Still following?

在 iOS 中,委托需要“委托”类来实现一个协议,其中包含“委托”知道的方法。还在关注?

The delegating class's implementation will call these protocol methods, but the delegate class will implement these methods in their class.

委托类的实现将调用这些协议方法,但委托类将在其类中实现这些方法。

This keeps your Classes clean.

这使您的班级保持清洁。

In reality, you don't really need delegation if you can add new methods to a single class. But for UIKIT's UIView class, Apple will not allow you to add new implementations to their class.

实际上,如果您可以向单个类添加新方法,则您实际上并不需要委托。但是对于 UIKIT 的 UIView 类,Apple 不允许您向其类添加新的实现。

correct me if I'm wrong.

如果我错了纠正我。

回答by Natasha

The most common use of a delegate in iOS is to establish communication within modules that are unrelated or partially related to each other. For example, passing data forward in a UINavigationController is very easy, we can just use segue. However, sending data backwards is little tricky. In this case, we can use delegate to send the data backward.

iOS 中委托最常见的用途是在彼此不相关或部分相关的模块内建立通信。例如,在 UINavigationController 中向前传递数据非常简单,我们可以使用 segue。但是,向后发送数据有点棘手。在这种情况下,我们可以使用委托向后发送数据。

Let's call, the class, associated with the first Controller ClassA and the class, associated with the second Controller ClassB. The first Controller is connected to the second controller with a forward segue. We can pass data from ClassA to ClassB through this segue. Now, we need to pass some data to ClassA from ClassB for which we can use delegates.

让我们调用与第一个控制器 ClassA 关联的类和与第二个控制器 ClassB 关联的类。第一个控制器通过前向连接连接到第二个控制器。我们可以通过这个 segue 将数据从 ClassA 传递到 ClassB。现在,我们需要将一些数据从 ClassB 传递给 ClassA,以便我们可以使用委托。

The sender class(ClassB) needs to have a protocol in its header file(.h) and also a reference of it as delegate inside the block, @interface ClassB .... @end. This reference let's the ClassB know that it has a delegate. Any class that wants to use this ClassB will have to implement all of this protocol's required methods(if any). So, the receiver class,ClassA will implement the method but the call will be made by the sender class, ClassB.

发送者类(ClassB)需要在其头文件(.h)中包含一个协议,并且在块中引用它作为委托,@interface ClassB .... @end。这个引用让 ClassB 知道它有一个委托。任何想要使用此 ClassB 的类都必须实现此协议所需的所有方法(如果有)。因此,接收者类 ClassA 将实现该方法,但调用将由发送者类 ClassB 进行。

This way, receiver class doesn't need to worry about the sender class' internal structure, and can receive the required information.

这样,接收者类就不需要关心发送者类的内部结构,就可以接收到需要的信息了。

回答by NSPratik

The class marked as delegate takes the responsibilities to handle the callbacks sent while some event occurs. For example, in case of UITextField, there are some methods called when some events occurs like editing started, editing ended, character typed etc. These methods will already be defined in the protocol. We will have to assign delegate for that i.e. which class is going to handle these events.

标记为委托的类负责处理在某些事件发生时发送的回调。例如,在 UITextField 的情况下,当某些事件发生时会调用一些方法,例如编辑开始、编辑结束、字符输入等。这些方法已经在协议中定义。我们必须为那个分配委托,即哪个类将处理这些事件。

回答by Vaibhav Shiledar

With the help of a delegate, two-way communication can be achieved. A delegate might be used to make an object reusable, to provide a flexible way to send messages, or to implement customization.

在代表的帮助下,可以实现双向通信。委托可用于使对象可重用、提供一种灵活的方式来发送消息或实现自定义。

回答by Chethan M

In iOS ecosystem especially UIKitFramework which consists of UIApplication, UITableView, UICollectionView, UITextfield& so on uses delegate & datasource design pattern intensively to communicate data to and fro.

在IOS生态系统特别UIKit框架它由UIApplicationUITableViewUICollectionViewUITextfield和等用途委派&数据源设计图案集中到数据通信来回。

Delegate design pattern is used to pass/communicate data from FirstVC(Delegator) to SecondVC(Delegate) to complete a task. Here, SecondVC(Delegate) conforms to a protocol delegate & implements all its requirements like methods by providing body to complete that task given by FirstVC(Delegator). Also, FirstVC(Delegator) object will be having a optional property of protocol delegate type i.e delegate which must be assigned by SecondVC(Delegate).

委托设计模式用于将数据从 FirstVC(Delegator) 传递/通信到 SecondVC(Delegate) 以完成任务。在这里,SecondVC(Delegate) 符合协议委托并通过提供主体来完成 FirstVC(Delegator) 给出的任务来实现其所有要求,例如方法。此外,FirstVC(Delegator) 对象将具有协议委托类型的可选属性,即必须由 SecondVC(Delegate) 分配的委托。

Now, FirstVC(Delegator) can call that method residing in SecondVC(Delegate) by passing data from its delegate property.

现在,FirstVC(Delegator) 可以通过从其委托属性传递数据来调用驻留在 SecondVC(Delegate) 中的方法。

EX: CEO(FirstVC) which passes data i.e "confidential data" to Secretary(SecondVC) to do further processes using that data.

EX:CEO(FirstVC)将数据(即“机密数据”)传递给秘书(SecondVC)以使用该数据进行进一步处理。

Datasource design pattern is part of Delegate pattern which is used to pass/communicate data from SecondVC(Delegate) to FirstVC(Delegator) when a task is assigned to SecondVC(Delegate). Here, SecondVC(Delegate) conforms to a protocol datasource & implements all its requirements like methods with return type by providing body to talk back to FirstVC(Delegator) after the task is given by FirstVC(Delegator). Also, FirstVC(Delegator) object will be having an optional property of protocol dataSource type i.e dataSource which must be assigned by SecondVC(Delegate).

数据源设计模式是委托模式的一部分,用于在任务分配给 SecondVC(Delegate) 时将数据从 SecondVC(Delegate) 传递/通信到 FirstVC(Delegator)。在这里,SecondVC(Delegate) 符合协议数据源并通过在 FirstVC(Delegator) 给出任务后提供主体与 FirstVC(Delegator) 进行对话来实现其所有要求,例如具有返回类型的方法。此外,FirstVC(Delegator) 对象将具有协议数据源类型的可选属性,即必须由 SecondVC(Delegate) 分配的数据源。

Now, FirstVC(Delegator) can call that method with a return type residing in SecondVC(Delegate) by passing data from its dataSource property.

现在,FirstVC(Delegator) 可以通过传递来自其 dataSource 属性的数据,使用位于 SecondVC(Delegate) 中的返回类型调用该方法。

EX: Secretary(SecondVC) replies back with a data i.e "Sir, I am already having too much work to do. Please, can you assign that data to others" to CEO(FirstVC). Now, CEO(FirstVC) will analyse that data to do further processes.

例如:秘书(SecondVC)回复了一个数据,即“先生,我已经有太多的工作要做。请您将这些数据分配给其他人”给首席执行官(FirstVC)。现在,CEO(FirstVC) 将分析这些数据以做进一步的处理。

回答by Emmett Butler

The main advantage of delegation over simply implementing methods in the "primary object" (by which I assume you mean the object doing the delegating) is that delegation takes advantage of dynamic binding. At compile time, the class of the delegate object does not need to be known. For example, you might have a class that delegates the windowDidMove:method. In this class, you'd probably see some bit of code like

与简单地在“主要对象”中实现方法(我假设您指的是执行委派的对象)相比,委派的主要优势在于委派利用了动态绑定。在编译时,不需要知道委托对象的类。例如,您可能有一个委托windowDidMove:方法的类。在这门课中,您可能会看到一些代码,例如

if([[self delegate] respondsToSelector:@selector(windowDidMove:)]) {
    [[self delegate] windowDidMove:notification];
}

Here, the delegating class is checking at runtime whether its delegate responds to the given method selector. This illustrates a powerful concept: the delegating class doesn't need to know anything about the delegate other than whether it responds to certain methods. This is a powerful form of encapsulation, and it is arguably more flexible than the superclass-subclass relationship, since the delegator and the delegate are so loosely coupled. It is also preferable to simply implementing methods in the "primary object" (delegating object), since it allows runtime alteration of the method's implementation. It's also arguable that this dynamic runtime makes code inherently more dangerous.

在这里,委托类在运行时检查其委托是否响应给定的方法选择器。这说明了一个强大的概念:委托类除了是否响应某些方法之外,不需要了解有关委托的任何信息。这是一种强大的封装形式,可以说比超类-子类关系更灵活,因为委托人和委托人是如此松散耦合。最好在“主要对象”(委托对象)中简单地实现方法,因为它允许在运行时更改方法的实现。同样有争议的是,这种动态运行时使代码本质上更加危险。

回答by Tunvir Rahman Tusher

Delegate is an important design pattern for iOS app.All apps directly or behind the hood use this delegate pattern. Delegate design pattern allows an object to act on behalf of another. If we are working with tableview then there are "tableViewDelegate" and "tableViewDataSource". But what this means

委托是 iOS 应用的重要设计模式。所有直接或幕后的应用都使用此委托模式。委托设计模式允许一个对象代表另一个对象进行操作。如果我们使用 tableview,那么有“ tableViewDelegate”和“ tableViewDataSource”。但这意味着什么

Suppose you have a tableview. now some major concern for this. 1.what is the datasource(the data that will appear in table view) for this tableview? 2.How many row for table view etc. delegate design pattern solve these question using another object as the provider or the solver of these question. An object mark himself to the table view and ensure the table view that "Yes i am the man who can assist you" by marking himself as the delegate to the table view .Thanks

假设你有一个 tableview。现在对此有一些主要关注。1.这个tableview的数据源(将出现在tableview中的数据)是什么?2.表视图等的多少行委托设计模式使用另一个对象作为这些问题的提供者或解决者来解决这些问题。一个对象将自己标记到表视图,并通过将自己标记为表视图的代表来确保表视图“是的,我是可以帮助您的人”。谢谢

回答by Michael

Delegation as I understand it is when an object will pass the responsibility of handeling an event to another object thus "delegating" the responsibility to that object.

据我所知,委托是指一个对象将处理事件的责任传递给另一个对象,从而将责任“委托”给该对象。

For example if you have an NSButton in iOs you generally assign the Delegate to be the parent view controller. This means instead of handeling touchUp events in the definition of the button it is instead handled in the view controller.

例如,如果您在 iOs 中有一个 NSButton,您通常会将 Delegate 指定为父视图控制器。这意味着不是在按钮定义中处理 touchUp 事件,而是在视图控制器中处理。