Data transfer via notification, iphone

Possible duplicate:
pass an NSString variable to another class using NSNotification

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

+6
source share
1 answer

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 get the dictionary from the incoming notification that you are observing. Add an observer before sending a notification.

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

it could be in your init method or viewDidLoad method

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

note that you must delete your object as an observer in the dealloc method.

 [[NSNotificationCenter defaultCenter] removeObserver:self] 
+18
source

Source: https://habr.com/ru/post/913977/


All Articles