简体   繁体   中英

What is the purpose of an iOS delegate?

I understand what a delegate does 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).

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.

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.

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

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

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.

correct me if I'm wrong.

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.

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.

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.

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

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.

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

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.

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

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

Delegation means one object passes behaviour to another object..

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 ie which class is going to handle these events.

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.

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

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 ie delegate which must be assigned by SecondVC(Delegate).

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

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

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 ie dataSource which must be assigned by SecondVC(Delegate).

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

EX: Secretary(SecondVC) replies back with a data ie "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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