使用Notification,iphone传递数据 [英] passing data using Notification,iphone
问题描述
我的问题是:我们是否可以使用postNotificationName和来自Iphone中通知类的addObserver将数据从一个视图控制器传递到另一个视图控制器
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
推荐答案
您可以在API调用的userDictionary元素中传递数据
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];
这可能在你的init方法或viewDidLoad方法中
this might be in your init method or a viewDidLoad method
-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}
请注意,您应该在dealloc方法中删除对象作为观察者。
note that you should remove your object as an observer in the dealloc method.
[[NSNotificationCenter defaultCenter] removeObserver:self]
这篇关于使用Notification,iphone传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!