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];