Cocoa design model notification

Source: Internet
Author: User

In the past, notifications were hard to handle and were always avoided. However, after a hard time, I found that it was really a good thing to use. Sort out the usage methods.

1. First, add a notification in viewDidLoad or elsewhere

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable:) name:@"updateTable" object:nil];
Nsicationicationcenter is a singleton mode class and has limited methods to call. You can check the api on your own. Addobserver: The following parameters are your current class or the class for receiving messages. Selector: The method to be executed after receiving the notification. The name is the name of the notification, and the object can be used as a parameter for message transmission.

Ii. Define the method to be executed after the notification is completed
-(Void) updateTable :( NSNotification *) sender {NSLog (@ "Notification successful"); NSDictionary * dictionary = (NSDictionary *) [sender userInfo]; NSArray * array = [[NSArray alloc] initWithArray: [dictionary objectForKey: @ "names"]; NSLog (@ "% @, % @", [array objectAtIndex: 0], [array objectAtIndex: 1]);}

Sender is the parameter passed during notification Publishing.

3. Publish a notification when you need to call a method

 [[NSNotificationCenter defaultCenter] postNotificationName:@"updateTable" object:nil userInfo:dictionary];

Dictionary is a previously defined parameter.

 NSArray *array = [[NSArray alloc] initWithObjects:@"foxbabe",@"philiphu", nil];    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:array,@"names", nil];

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.