So i have an app with an In App purchase. The In App purchase is managed in FirstViewController. When the user has purchased the product, i want to send out a Notification to my MainTableViewController to reload the tables data and show the new objects that were purchased in the In App purchase. So basically i want to send a notification from class A to class B and class B reloads the data of the tableview then. I have tried using NSNotificationCenter, but with no success, b开发者_JAVA百科ut i know that its possible with NSNotificationCenter i just don't know how.
In class A : post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:self];
In class B : register first for the notification, and write a method to handle it.
You give the corresponding selector to the method.
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(@"recieved");
[self.tableView reloadData];
}
Ok I'm adding a little bit more information to vince's answer
In class A : post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:arrayOfPurchasedObjects];
In class B : register first for the notification, and write a method to handle it.
You give the corresponding selector to the method. Make sure your class B is allocated before you post the notification otherwie notification will not work.
- (void) viewDidLoad {
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(@"recieved");
NSArray *purchased = [notification object];
[classBTableDataSourceArray addObjectsFromArray:purchased];
[self.tableView reloadData];
}
- (void) dealloc {
// view did load
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"DataUpdated"
object:nil];
[super dealloc];
}
Maybe you trying to send notification from another thread? NSNotification won't be delivered to the observer from another thread.
精彩评论