What is the purpose of an iOS delegate?

前端 未结 11 1482
野性不改
野性不改 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: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.

提交回复
热议问题