IOS development UI-notification center (nsicationicationcenter), iosnotification

Source: Internet
Author: User
Tags notification center

IOS development UI-notification center (nsicationicationcenter), iosnotification

1. notification center)

 

 

Each application has an nsicationicationcenter instance dedicated to assisting different objects.

Inter-Message Communication

• Any object can publish a notification (NSNotification) to the notification center to describe what it is doing. Other Objects of interest (Observer) can apply to publish a specific notification (or when a specific object publishes a notification)

 

2. Notification (NSNotification)

• A complete notification generally contains three attributes:

-(NSString *) name; // notification name

-(Id) object; // notification publisher (who wants to publish a notification)

-(NSDictionary *) userInfo; // some additional information (the content of the information that the notification publisher delivers to the notification recipient)

• Initialize an NSNotification object

Ø + (instancetype) notificationWithName :( NSString *) aName object :( id) anObject;

Ø + (instancetype) notificationWithName :( NSString *) aName object :( id) anObject userInfo :( NSDictionary *) aUserInfo;

-(Instancetype) initWithName :( NSString *) name object :( id) object userInfo :( NSDictionary *) userInfo;

• A complete notification generally contains three attributes:

-(NSString *) name; // notification name

-(Id) object; // notification publisher (who wants to publish a notification)

-(NSDictionary *) userInfo; // some additional information (the content of the information that the notification publisher delivers to the notification recipient)

• Initialize an NSNotification object

Ø + (instancetype) notificationWithName :( NSString *) aName object :( id) anObject;

Ø + (instancetype) notificationWithName :( NSString *) aName object :( id) anObject userInfo :( NSDictionary *) aUserInfo;

-(Instancetype) initWithName :( NSString *) name object :( id) object userInfo :( NSDictionary *) userInfo;

3. Release notification

The notification center (nsicationicationcenter) provides methods to help publish notifications •-(void) postNotification :( NSNotification *) notification;

Publish a notification. You can set the notification name, publisher, and out-of-quota information in the notification object.

•-(Void) postNotificationName :( NSString *) aName object :( id) anObject; Ø publish a notification named aName, and anObject is the publisher of the notification

•-(Void) postNotificationName :( NSString *) aName object :( id) anObject userInfo :( NSDictionary *) aUserInfo;

Publish a notification named aName. anObject is the publisher of the notification, and aUserInfo is the additional information.

4. register the notification listener

The notification center (nsicationicationcenter) provides a method to register an listener (Observer) that listens to notifications)

•-(Void) addObserver :( id) observer selector :( SEL) aSelector name :( NSString *) aName

Object :( id) anObject;

Ø observer: Listener, that is, who wants to receive the notification. Ø aSelector: This method is called back after the notification is received, and the notification object is passed as a parameter. Ø aName: the notification name. If it is nil, the listener will receive this notification regardless of the notification name. Ø anObject: the notification publisher. If both anObject and aName are nil, the listener receives all notifications.

•-(Id) addObserverForName :( NSString *) name object :( id) obj queue :( NSOperationQueue *) queue usingBlock :( void (^) (NSNotification * note) block;

Ø name: Notification name

Ø obj: Notification publisher Ø block: This block is called back when a corresponding notification is received.

Ø queue: determines the operation queue in which the block is executed. If nil is passed, it is executed synchronously in the current operation queue by default.

5. cancel registration notification listener

• The notification center does not retain the retain listener object. Objects registered in the notification center must be removed from registration before the object is released. Otherwise, when the corresponding notification appears again, the notification center will still send a message to the listener. Because the listener object has been released, the application may crash.

• The notification center provides methods to cancel the registration of the listener (void) removeObserver :( id) observer;

(Void) removeObserver :( id) observer name :( NSString *) aName object :( id) anObject;

• Generally cancel registration before the listener is destroyed (for example, add the following code to the listener):-(void) dealloc {

// [Super dealloc]; [[nsicationicationcenter defaultCenter] removeObserver: self] must be called in non-ARC statements;

}

6. UIDevice notification

• The UIDevice class provides a single object, which represents a device and can be used to obtain device-related information, such as batteryLevel and batteryState), device type (model, such as iPod, iPhone, etc.), device system (systemVersion)

• This single object can be obtained through [UIDevice currentDevice]

• The UIDevice object will continuously publish some notifications. The following are the name constants of notifications published by the UIDevice object:

Ø UIDeviceOrientationDidChangeNotification // rotate the device

Ø UIDeviceBatteryStateDidChangeNotification // The Battery status changes.

UIDeviceBatteryLevelDidChangeNotification // change battery power

Ø UIDeviceProximityStateDidChangeNotification // close sensor (for example, the device is close to the user's face)

VII. keyboard notification

• When the system sends a keyboard notification, additional keyboard-related information (dictionary) is attached. The common keys in the dictionary are as follows:

When the keyboard status changes, the system will send some specific notifications. Ø UIKeyboardWillShowNotification // the keyboard is about to display. Ø UIKeyboardDidShowNotification // the keyboard is displayed. Ø uikeyboardwillenoenotification // the keyboard is about to be hidden. Summary // the keyboard is hidden. the position and size of the keyboard are about to change. Ø UIKeyboardDidChangeFrameNotification // The position and size of the keyboard have been changed.

Ø

UIKeyboardFrameBeginUserInfoKey // frame at the beginning of the keyboard

Ø UIKeyboardFrameEndUserInfoKey // The final frame of the keyboard (after the animation is executed)

Ø UIKeyboardAnimationDurationUserInfoKey // time of keyboard Animation

Ø UIKeyboardAnimationCurveUserInfoKey // The execution speed (speed) of the keyboard Animation)

8. Notification and proxy Selection

• Commonalities

Ø communication between objects can be completed using notifications and Proxies (for example, object A tells object D what happened and object A passes data to object D)

• Differences

Ø Proxy: one-to-one relationship (one object can only tell the other object what happened)

Notification: many-to-many relationship (one object can tell N objects what happened, And one object can know what happened to N objects)

 

 

 

 

 

 

 

 

 


What is the role of nsicationicationcenter in iOS?

First, these two things are not in the same framework,
Nsicationicationcenter supports MacOS in/System/Library/Frameworks/Foundation. framework.
UILocalNotification is available in/System/Library/Frameworks/UIKit. framework iOS.
UILocalNotification can be understood as a Local implementation of RemoteNotification. It is a UI-oriented notification mechanism (supporting Alert, Sound, and so on. For example, when the timer ends, a notification is sent and a prompt is displayed.
Nsicationicationcenter is an event-oriented notification center. Its principle is that you register a notification event to the defaultCenter. The event identifier is myName, when a notification of myName is Post to the notification center, myFunc: In all lifecycle will be executed.

Use objective-C to implement a message center

The most basic method for communication between objects is message transmission. In Cocoa, the Notification Center mechanism is provided to complete this task. Its main function is to communicate between any two objects. It is easy to use. follow these steps:

Assume that A communicates with B, B triggers the event, and A accepts the event and responds to it.
1) A write A custom message response function update
2) A registers with the message center. [nsicationicationcenter defacenter center] addObserver: self selector: @ selector (update) name: @ "update" object: nil]
3) B. Trigger the event [[NSNotificationCenter defacenter center] postNotificationName: @ "update" object: nil]

Each process has a default nsicationicationcenter. You can use the defacenter center class method to obtain the instances of this message center. The message center can process messages between different objects in the same process. If you want to communicate between processes on the same machine, you need to use NSDistributedNotificationCenter.

The message center distributes messages synchronously to all the observers. In other words, the control is returned to the caller after all the observers receive the messages and process the messages. To process messages asynchronously, use the notification queue NSNotificationQueue.

In a multi-threaded program, notifications are distributed to every thread that initiates messages. This may be different from the thread where the observer registers.

Instance:
@ Implementation TestClass

-(Void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// Will continue to try and send notification objects to the deallocated
// Object.
[[Nsicationcenter center defacenter center] removeObserver: self];
[Super dealloc];
}

-(Id) init
{
Self = [super init];
If (! Self) return nil;

// Add this instance of TestClass as an observer of the TestNotification.
// We tell the notification center to inform us of "TestNotification"
// Configurications using the receiveTestNotification: selector.
// Specifying object: nil, we tell the notification center that we are not
// Interested in... the remaining full text>

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.