IOS observer mode and notification center

Source: Internet
Author: User
Tags notification center

The observer mode of IOS is not as frequently used as the proxy method in IOS. In most cases, from tableView to textField, both define proxy and implement proxy for function callback.

The observer mode can only be used when the notification center is used. Is the observer mode inferior to the proxy mode? Although the observer mode is less efficient than the proxy mode, the observer mode can be used to decouple the program and it is more convenient to use the observer mode than layer-by-layer proxy in a deep hierarchy.

----------------------------

Next, we will introduce the observer mode:

Observer mode:

The observer mode defines the method callback mode in one-to-many cases. In general, the observer mode is used to remove the coupling between one object and multiple objects, namely the observer and the observer. The coupling between the observer and the observer is reduced. No matter how many listeners there are, the code of the listener does not need to be changed.

The basic process is:

The observer registers as the observer of the Observer. When the observer changes, the listener is triggered and the listener method in the observer is called. The observer usually uses a container to store the listener. When it changes, it notifies all the listeners in the container.

---------------------------

Implementation of the observer mode in IOS:

The observer mode is very simple. The Subject (topic) allows other objects-The Observer (these objects implement the observer Interface) to subscribe to and unsubscribe to the changes of this Subject. When the Subject changes, the Subject sends the change to all the observers, and the observer can update the change. Here, the Subject is the publishing house of the newspaper, while the observer is the one who subscribes to the newspaper and me. When the Subject changes-there is a new newspaper, a notification will be sent-to send the newspaper to all subscribers.

When to use the observer mode:

When you need to notify all objects of changes without knowing the specific types of these objects, you can use the observer mode. Changes occur in the same object and must be updated elsewhere.

Implementation of the observer mode in IOS:

In iOS, the observer mode has three methods: Notification, KVO, and standard.

1. Notification (use the Notification center provided by the system)

Icationicationcenter uses the functions of the operating system. The nsicationicationcenter allows communication between objects, which can be unknown to each other. This is useful when you use a parallel stream to push notifications, or refresh the database, and want to see it on the interface. (We can use this method when listening to certain system events. For example, when the keyboard is lifted and dropped, a textfield will send a notification to the notification center when it loses its first responder, by listening to these notifications, we can interact with the system)

1. NSNotification * broadcastMessage = [NSNotification notificationWithName: AnyNotification object: Self];
2. nsicationicationcenter * notificationCenter = [NSNotificationCenter defacenter center];
3. [icationcenter center postNotification: broadCastMessage];

In the above Code, an NSNotification type object is created, and the name is "broadcastMessage" is specified. Then, the message is published through icationicationcenter.


To subscribe to related events in objects of interest, follow these steps:

1. nsicationicationcenter * notificationCenter = [NSNotificationCenter defacenter center];
2. [icationicationcenter addObserver: Self selector: @ selector (update :) name: AnyNotification object: nil];

2. KVO

Key-value observer: in this way, when a property of an object changes, those registered listeners will receive a notification.

For example, we want to listen to the text of a UIlabel. A method is triggered when the text changes. UIlabel * label;

NSObject included in KVO

-(Void) ObserveValueForKeyPath :(NSString*) KeyPath ofObject :(Id) Object change :(NSDictionary*) Change context :(Void*) Context

Method. This method is called when the object listens for notifications.

--------

That is to say, we can listen to notifications by overwriting any subclass inherited from NSObject. Note that there are any notifications. In this method, different notifications must be distinguished by judgment.

We know from this: in fact, any class inherited from the Object can listen to notifications, so any Object can be addObsever to a Monitored Object, and then in the Object'sObserveValueForKeyPath for subsequent operations.

3: manual notification:

In the preceding method, automatic notifications are defined by the system. When a property changes, the system automatically sends a notification to the listener, if we need to further define the time when the notification is sent, we should use the method of manually sending the notification.

willChangeValueForKey:
didChangeValueForKey:
The two methods will send notifications for the specified key.

4: automatic notification:

[self setValue:[NSNumber numberWithBool:YES]forKey:@"isFinished"]; 
After registering the observer with the observer, you can perform operations on the attributes of the observer object, and these changes will be notified to the observer object. Note: The Observer object will receive notifications only when the attributes are set in KVO mode. That is to say, the object follows the setter method of the attribute or uses key-path to set the attributes:
Automatic notification can be disabled. The automaticallyNotifiesObserversForKey method is used to set automatic notification for certain key values. By default, automatic notification is triggered for all attributes and no is returned by setting a key. When manual notification is used, automatic notification is usually disabled.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.