iOS core note--ios notification mechanism

Source: Internet
Author: User

1, IOS notification mechanism: 1-1, iOS notification publisher, Notification Center, the relationship between listeners:

as shown in the following:

2, notice (nsnotification) Introduction:

3, Notification Center (nsnotificationcenter) 3-1, each application has only one notification hub object (Singleton object):

4, Release Notice: 4-1, the object at a time when they need to use the notification class (Nsnotification) to notify the Notification Center to help publish the notification:
    • 1, can be in the object a need to use a notification location, direct Notification Center to help send a series of information in the notification through the Notification Center provides a method to create a notification and send out simultaneously:

      sample program :

           1.-(void) postnote{
      2. MARK:-Send notification
      3. [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "HF" Object:self userinfo:@{
      4. @ "name": @ "He Hua",
      5. @ "Birthday": @ "1994-01-19"
      6. }];
      7.}

       

    • 2, first create the notification object, set the notification information when it is created, and then let the notification Center help send:

      sample program :

        1.-(void) postnote2{
      2. 1. Create a notification
      3. Nsnotification *note = [nsnotification notificationwithname:@ "HF2" Object:self userinfo:@{
      4. @ "name": @ "He Hua 2",
      5. @ "Birthday": @ "01-19"
      6. }];
      7.//2. Notification Center to help send
      8. [[Nsnotificationcenter Defaultcenter] postnotification:note];
      9.}

       

    • 3, common methods for posting notifications:
5, monitoring notification: 5-1, the listener only after the Notification Center registration monitoring notification, to receive notification after the announcement, and listener registration monitoring must be in the notification before the release; 5-2, listener Registration Monitoring notification common way:
    • Way One:
    • Way two:
6, Cancellation: 6-1, the listener must be written off before destroying the notification, otherwise, will cause the problem of wild pointers; 6-2. How the listener logs off the notifications it listens to:

7, keyboard notification: 7-1, common keyboard notification type:

7-2, keyboard notification commonly used additional information:

8, the common keyboard notification: 8-1, the keyboard will be displayed:

8-2. The keyboard will be hidden:

8-3, the keyboard frame will be changed:

9, iOS device class notification: 9-1, UIDEVICE class notification:

10, Comparison notice and agent: 10-1, Agent:
    • 1, the role of agent design mode:

      • 1.A object listens to some behavior of B object, a becomes agent of B;
      • 2.B objects want to tell a object something, a becomes a proxy for B.
    • 2, the agent Design Model summary:

      • If you want to listen to other people's behavior, then you will be the agent of others;
      • If you want to tell someone something, then let someone else be your agent.
    • 3, the agent design mode development steps:

      • 1. To prepare an agreement (the format of the agreement name: control name + Delegate), in the agreement to declare some proxy methods (the general Agent method is @optional);
      • 2. Declare a proxy property: @property (nonatomic, weak) id< Agent agreement > delegate;
      • 3. When some internal behavior occurs, call the agent corresponding Agent method, notify the agent what happened inside;
      • 4. Set Proxy: Xxx.delegate = YYY;
      • The 5.yyy object adheres to the protocol and implements the proxy method.
    • 4, the difference between agent and notice:

      • Agent: An object can only tell the other 1 objects what happened;

      • Notification: An object can tell what happened to n objects.

10-2, the choice of notification and agent:

iOS core note--ios notification mechanism

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.