通过NSNotificationCenter向另一个类通知
-
25-10-2019 - |
题
因此,我的目标是通过使用 NSNotificationCenter
, ,我也想通过 object
并通知对方 class
, ,我应该怎么做?
解决方案
您必须首先注册通知名称
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":"
然后发布带有参数字典的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]];
该方法将是
- (void)startLocating:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
}
其他提示
只需调用任何按照所述发布通知的方法 这里, , 例如 :
发布通知:
-(void)postNotificationName:(NSString *)notificationName
object:(id)notificationSender
userInfo:(NSDictionary *)userInfo;
在哪里 userInfo
是包含有用对象的字典。
在另一侧注册通知:
-(void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender;
你也可以检查苹果的 通知编程主题.
不隶属于 StackOverflow