(72) Custom notification nsnotification for message delivery

Source: Internet
Author: User

As is known to all, iOS generally pass messages between classes using more delegate and blocks, there is a notification-based message delivery, we often use the system's notification to implement some functions, such as the use of keyboard size change notification, We can change the position of the input box according to the position of the keyboard, so as to avoid the input box being obscured by keyboard.

In addition to the use of system notifications, we can also create a notification by ourselves to complete a number of message delivery, as an example of XMPP login, if the user did not log off before the start of the client should be automatically logged in, login in the XMPP tool class completed, However, the current view should be rotated with the network connection indicator (Activity Indicator) before the login is completed, and the indicator should be hidden after the connection is completed, and when the connection is successful, the tool class notifies the view controller.

Notification to implement very simple, only need to listen and send, recall that the listening keyboard, you need to specify the name of the notification, the standard format is the class name + function +notification, longer, so a constant string to get the class name, to prevent the input error.

① Create a constant string:

Write directly on the. m header that sent the notification:

NSString *const wcloginstatuschangenotification = @ "Wcloginstatusnotification";
It is then declared in. h to invoke when listening for notifications:

extern nsstring *const wcloginstatuschangenotification;
② let the view controller listen for notifications:

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (loginstatuschange:) Name: Wcloginstatuschangenotification Object:nil];
Then implement the listening method, the parameters areThe Nsnotification object, which has a UserInfo member property used to pass the message, is a dictionary.

③ Send a notification:

[[Nsnotificationcenter Defaultcenter] postnotificationname:wcloginstatuschangenotification object:nil userInfo: < dictionaries >];
The pass parameter takes advantage of the UserInfo parameter, and only needs to be assigned a dictionary to be passed to the listener's event function.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(72) Custom notification nsnotification for message delivery

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.