What is the purpose of an iOS delegate?

前端 未结 11 1473
野性不改
野性不改 2020-12-18 23:22

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 includin

相关标签:
11条回答
  • 2020-12-18 23:26

    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.

    0 讨论(0)
  • 2020-12-18 23:28

    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.

    0 讨论(0)
  • 2020-12-18 23:30

    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.

    0 讨论(0)
  • 2020-12-18 23:35

    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.

    0 讨论(0)
  • 2020-12-18 23:38

    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

    0 讨论(0)
  • 2020-12-18 23:40

    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.

    0 讨论(0)
提交回复
热议问题