Message transmission mechanism (KVO, Notification, delegation, block, and target-action) in iOS --- reprint, kvotarget-action
Note 1: This article is translated from Communication Patterns by the [blog. The Directory of this article is as follows:Available MechanismMake the right choiceFramework example SummaryEach application is more or less composed of loosely coupled objects. to complete tasks well between these objects, message transmission is required. This article will introduce all the available message passing mechanisms, introduce how these mechanisms are used in Apple's Framework through examples, and introduce some best practices and suggestions, tell you when to choose the mechanism to use. Although the topic of this phase is about the Foundation Framework, this article also introduces some messaging mechanisms beyond the scope of the Foundation Framework (KVO and Notification) and delegation, block and target-action. In most cases, it is very clear what mechanism should be used for message passing. Of course, in some cases, there is no clear answer to what mechanism to use. You need to try it yourself. In this article, the receiver [recipient] and the sender [sender] are often mentioned. In the message transmission mechanism, we can use an example to explain how a table view is a sender, and its delegate is the receiver. Core Data managed object context is the sender of the notification, and the receiver obtains the notification. A slider is the sender of the action message, and the responder corresponding to the implementation of this action in the Code is the receiver. If a property in an object supports KVO, then Who modifies the value, who is the sender, and the corresponding observer (observer) is the receiver.Available MechanismFirst, let's look at the specific characteristics of each mechanism. In the next section, I will introduce how to select the correct message transmission mechanism based on a flowchart. Finally, we will introduce some examples from the apple Framework and explain why a fixed mechanism should be selected under certain conditions.KVOKVO provides a mechanism to notify the observer of a property value in an object when it changes. The implementation of KVO is included in the Foundation. Many frameworks built based on the Foundation depend on KVO. To understand