Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing data using Notification,iphone [duplicate]

Tags:

ios

iphone

Possible Duplicate:
pass NSString variable to other class with NSNotification

My question is : is it possible we can pass data from one to another view controller using postNotificationName and addObserver from notification class in Iphone

like image 394
tranvutuan Avatar asked Apr 23 '12 20:04

tranvutuan


1 Answers

You can pass data in the userDictionary element of the API call

NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                          anObject, @"objectName", 
                          anotherObject, @"objectId",
                          nil] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary];

You can retrieve the dictionary from the inbound notification that you observe. Add the observer in advance of posting the notification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil];

this might be in your init method or a viewDidLoad method

-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}

note that you should remove your object as an observer in the dealloc method.

[[NSNotificationCenter defaultCenter] removeObserver:self]
like image 141
Warren Burton Avatar answered Sep 30 '22 17:09

Warren Burton