In objective-C, the boss sends a notification in this way)

Source: Internet
Author: User
Tags notification center

? ? The notification is a simple analogy. the boss of the company sent a notification to the following employees, saying that the company will go public tomorrow and all departments should make preparations. After the notification is sent, each department will perform their respective duties and do what they should do. If the boss sends a notification through the company's internal forum, the boss is the recipient of the notification, and the company employee is the recipient of the notification, and the company's internal forum is the notification center, to receive notifications sent by the boss, register on the Forum first. There is only one boss, and there are multiple departments that receive notifications. The boss sends notifications through Forum broadcasts. A notification is a message sent to one or more observations to notify the program of an event. Notifications in cocoa follow a broadcast mode in a timely manner.

? ? For the time being, this is an example I have come up with based on my personal understanding. There may be some shortcomings. I hope you will criticize and correct them. For more information, see the source.

? ? To put it bluntly, how can we use the notification in objective-C to send notifications in the form of Forum posts by the boss?

? ? 1. A company must have a boss, right? So we need to create a new boss class. The internal forum of the company is the notification center (icationicationcenter) in our OC. After reading the boss's implementation code, let's say a few words.

1234567891011121314 @implementation Boss-(void)sendMessage{    // Put the information to be sent into the dictionary (what the boss wants to say in the Forum)    NSDictionary *message = @{@"notification" : @"Please note that the company will be listed tomorrow! "};         // Create a notification object (the boss logs on to the boss account)    NSNotification * notification = [NSNotification notificationWithName:@"boss" object:self userInfo:message];         // Send a message to the notification center (publish a message)    [[NSNotificationCenter defaultCenter] postNotification:notification];     }@end

? ? Code Description:

? ? ? ? 1. messages to be sent by the boss can be published only in the dictionary. The dictionary key is the topic of the post and the value is the content of the post.

? ? ? ? 2. Creating a notification object is like logging on to the internal Forum as the boss, and using icationwitwithname to set the nickname of the boss

? ? ? ? 3. The boss posts a message to the notification center.

 

? 2. Next we will start to simulate what each department will do after receiving the notification. What the human resources department will do after receiving the notification

12345678910111213141516171819202122 @implementation HumanResources- (instancetype)init{    self = [super init];    if (self) {        // Register at the notification center and confirm the message (log on to the Forum and follow the boss)        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"boss" object:nil];    }    return self;} -(void)doSomething:(NSNotification *)notification{         // Receive the message (the boss message is displayed on the Forum)    NSDictionary *bossInfo = [notification userInfo];    // Output the received information    NSLog(@"HR department received: % @", bossInfo[@"notification"]);}@end

? Code Description:

? ? 1. registering as an observer is equivalent to registering a department in an internal forum and paying attention to the boss. while paying attention to the boss, you must specify what to do when the boss sends a notification.

? ? 2. What each department does in dosomething

? 3. Add another Finance Department, which is similar to the above Code.

? 4. perform the following test in the main function:

12345 Boss * boss = [[Boss alloc] init];Finance * finance = [[Finance alloc] init];HumanResources *hr = [[HumanResources alloc] init];// The boss sent a message[boss sendMessage];

? When the boss sends a notification, each department will automatically invoke the automatic execution method. The running result is as follows:

12 15:10:29. 816 memory [1946: 303] The finance department received the message: Please note that the company will be listed tomorrow!15:10:29. 817 memory [1946: 303] The Human Resources Department received the message: Please note that the company will be listed tomorrow!

? For the time being, the above summary is based on the author's own understanding, which is biased. You are welcome to criticize and correct the content. For more information, see the source.

 

In objective-C, the boss sends a notification in this way)

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.