iOS notification hubs

Source: Internet
Author: User

The messaging mechanism in iOS development is generally subdivided into three broad categories:
    • Local notifications

    • Push Notifications

    • Notification Center

There is no direct connection between the three, Notification Center is to register an observer in the application of all objects broadcast messages, through the notification center can be well coordinated between different objects of message communication.

Attention:

Each application has only one notification hub (Nsnotificationcenter) instance

Both agent and notification hubs coordinate message communication between objects, and notification hubs are functionally similar to proxies, what are the distinctions between the two choices?

The agent corresponds to the relationship of one to the other, and the notification is a a-to-many relationship, so when a change is encountered it causes more
Changes in the functionality of our best choice of notifications.

For example:
    1. Change skin effect on QQ Music app

    2. Reading software changes font size effect

reasonable use of both in the actual development to prevent the overqualified

A notice of the successful implementation of the three elements must be
    1. Notify Publishers

    2. Notification Center

    3. Notification recipients

A full notification consists of three attributes
    1. Name: Names of notifications

    2. Object: The publisher of the notification

    3. UserInfo: The information content of the notifier to the notifying recipient

Notification recipient Registration notification method to notification center
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;  observer:监听器,即谁要接收这个通知aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入 aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知 anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
Notification hubs three ways to publish notifications
? - (void)postNotification:(NSNotification *)notification;? - (void)postNotificationName:(NSString *)aName object: (id)anObject;? - (void)postNotificationName:(NSString *)aName object:(id)anObjectuserInfo:(NSDictionary *)aUserInfo;
Dealloc method under Notification

After we register in the Notification Center, the notification center is very sensitive, when the condition is triggered will continue to send the message to the registrant, but when our registrant has been released, this time will cause a wild pointer, triggering the program crashes, so when our object is released, the object should be released to notification Center registration notification.

    - (void)dealloc    {        [[NSNotificationCenter defaultCenter] removeObserver:self];     }
In addition, there are two ways to unregister:
- (void)removeObserver:(id)observer;- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;//第二个多了一个name的参数可以具体到该对象中某个通知
A common practical application of notification hubs

1. Device rotation

    UIDeviceOrientationDidChangeNotification

2. Keyboard operation

Remind

The order of normal execution of the notification is: the recipient registers the notification in the Notification Center and the notifier posts the notification in the Notification Center ——— > releases the Notification Center's registration notice. An error in order will occur.

iOS notification hubs

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.