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, but i know that its possible with NSNotificationCenter i just don't know how.
Send NSNotification from classA to classB
Asked Answered
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];
}
well, the -(void)updated {[self.tableView reload data];} does not get called when the notification is sent, please check your code, ive tried modifying the code but it didn't help. –
Lawn
@Maxner - the problem is surely elsewhere check out Apple's Notification Programming Topics, you will see notifications system is easy as I wrote. –
Fist
ok, still thank you for the code, i will upvote both yours and Rahul Vyas answer. –
Lawn
@Maxner - please make sure using this code you are effectively registering for the notification at some point. you could easily check it by logging some message just before. did you put the registration method call in
viewDidLoad
in MainTableViewController
? –
Fist @Maxner - so 2 last points to check : 1) do the two classes post and register for the same notification names ("DataUpdated" in the example) ? 2) does the selector (@selector(handleUpdatedData:)) correspond to the method name (-(void)handleUpdatedData:(NSNotification *)notification;) ? –
Fist
yes everything is spelled correctly, ive also made it easier, by calling the selector "b:" and the notification name "a", problem is still there... –
Lawn
@Maxner - really, really interesting, is the notification really posted ? did you put a NSLog call just before posting it ? –
Fist
yes it is being posted, i first put it into SKPaymentTransactionStatePurchased: it got called for sure there along with an NSLog and UIAlertView, now i have just connected the notification to an ibaction of a button to make testing faster, its getting called too and nothing is happening. i dont understand it... –
Lawn
If it helps: ClassA is a UIViewController, classB is a UITableViewController. the view of ClassB is a subview of classA's view. –
Lawn
@Maxner - yes a bit, try to register for the notification in your
UITableViewController
subclass initializer. –
Fist do you mean the - (id)initWithStyle:(UITableViewStyle)style method? Doesn't work either. –
Lawn
@Maxner - but what isn't working ? are the method calls reached at some point, did you verify it ? look at the link in my answer, it should work like that. –
Fist
The method name is misspelled: The correct methodname should not be: postNotficationName but: postNotificationName (with an extra i). Maybe this solves the problem? (I cannot edit the post as edits should be more than 6 characters..) –
Eudocia
not sure of best method, but in other stackoverflow code, i've seen in class B, register for notification in viewWillAppear and remove observer in viewWillDisappear. –
Totipalmate
Don't forget to remove the notification observer in viewWillDisappear. –
Polis
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];
}
thx but its not working either, also i use NSUserdefaults to recognize if the in app purchase is complete, so i dont need to add objects to the array. –
Lawn
for new folks (me), there is a typo in answer above, 'postNotficationName' throws an error, 'postNotificationName' (with another 'i') corrects it. (thank you Rahul Vyas for helping to clarify notifications process). –
Totipalmate
There is a typo in your answer @RahulVyas , it is not "postNotficationName" it is "postNotificationName" –
Polis
Maybe you trying to send notification from another thread? NSNotification won't be delivered to the observer from another thread.
Try to compare [NSThread currentThread] in place where you generate notification to [NSThread mainThread] in class which observe for notification. –
Casseycassi
Also make sure, what you not specified object when adding observer. –
Casseycassi
© 2022 - 2024 — McMap. All rights reserved.
reloadData
will make the tableView asking its datasource for the data again. – Fist