传递数据使用通知,iphone

可能重复:
使用NSNotification将NSStringvariables传递给其他类

我的问题是:是否有可能我们可以传递数据从一个到另一个视图控制器使用postNotificationName和addObserver从通知类在iPhone

您可以在API调用的userDictionary元素中传递数据

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

您可以从您观察的入站通知中检索字典。 在发布通知之前添加观察者。

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

这可能在你的init方法或viewDidLoad方法中

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

请注意,您应该在dealloc方法中将其作为观察者移除。

 [[NSNotificationCenter defaultCenter] removeObserver:self]