我的问题是:我们是否可以使用postNotificationName和iphone中通知类的addObserver将数据从一个视图控制器传递到另一个视图控制器
War*_*ton 18
您可以在API调用的userDictionary元素中传递数据
NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
anObject, @"objectName",
anotherObject, @"objectId",
nil] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary];
Run Code Online (Sandbox Code Playgroud)
您可以从您观察到的入站通知中检索字典.在发布通知之前添加观察者.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil];
Run Code Online (Sandbox Code Playgroud)
这可能在您的init方法或viewDidLoad方法中
-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}
Run Code Online (Sandbox Code Playgroud)
请注意,您应该在dealloc方法中删除作为观察者的对象.
[[NSNotificationCenter defaultCenter] removeObserver:self]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8256 次 |
| 最近记录: |