Reputation: 990
I have in my app a UITableview Controller, a View Controller and I'm trying to pass NSDictionary from UITableview Controller to my ViewController, using NSNotificationCenter. So, I push a notification at my UITableview Controller and then I add an observer ,using a selector at my ViewController.The selector is called,but I have an NSLog and get memory results ,like :
ViewController: 0x8a0bcc0
I have tried to pass NSString instead of NSDictionary , but I get again memory results , and not the value of the string.
My code :
UITableView Controller
NSString *string=@"This is a test string";
[[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string];
ViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self];
And here is the incomingNotification selector method:
-(void) incomingNotification : (NSNotification *)notification{
NSLog(@"Trying to print : %@",[notification object]);
}
All Notifications take place at ViewDidLoad method.Thank you!
UPDATE
Finally , I quit using NSNotificationCenter and used properties to pass data ,changing a bit the inheretence from my TableViewController. No idea why Notifications did not work ,as they were supposed to. Thank you all ,very much for your suggestions and ideas :)
Upvotes: 1
Views: 323
Reputation: 4510
If I understand correctly, UIViewController
is shown after you tap a button on UITableViewController
. And you if you are adding a ViewController
as observer in its -viewDidLoad:
, then it will be able to receive notifications only when it is loaded.
What do you need:
1) override -init
or -initWithNibName:
method of ViewController
like this:
-(id) init
{
self = [super init];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incomingNotification:) name:@"Update" object:nil];
}
return self;
}
so you can be sure ViewController
is observing for notifications from the beginning (well, this might be unnecessary step for your case)
2) when you push ViewController
you need to send a notification after it was created, like this:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *nextController = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
NSString *string=@"This is a test string";
[[NSNotificationCenter defaultCenter] postNotificationName: @"Update" object: string];
}
However, if you're trying just to send some parameters from one view controller to another, this is the wrong way. Just create a property in ViewController
and in method -tableView:didSelectRowAtIndex:
of UITableViewController
set this property
Upvotes: 0
Reputation: 1570
[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self]
Object means the object that generates a notification. To post parameters use another method
[[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self userInfo:string]
Upvotes: 1