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