因此,我的目標是使用NSNotificationCenter
向另一班級發送通知,我還想通過object
並將通知發送給其他class
,我應該如何執行此操作?使用NSNotificationCenter向其他班級發出通知
3
A
回答
7
您必須先註冊一個通知名
[[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];
}
0
只需撥打任何方法如所描述的here,例如發佈通知:
來發布通知:
-(void)postNotificationName:(NSString *)notificationName
object:(id)notificationSender
userInfo:(NSDictionary *)userInfo;
其中userInfo
是含有有用的對象的字典。
在對方註冊通知:
-(void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender;
你也可以查看蘋果的Notification Programming Topics。
相關問題
- 1. 使用其他班級的班級
- 2. Java。從其他班級執行班級?
- 3. 向其他班級投入一個值
- 4. C#使用班級其他班級的數據
- 5. 使用NSNotificationCenter接收通知
- 6. 我的班級如何知道其他班級不應該如何?
- 7. 找到班級的高度,並適用於其他班級
- 8. 給予班級成員對其他班級成員的引用
- 9. NSNotificationCenter:是否有可能監聽其他未明確發佈通知的類?
- 10. 讓班級脫離其他班級智能(Obj-C)?
- 11. 在mouseenter上,增加班級,但刪除所有其他班級
- 12. Java - 讓其他班級需要的班級
- 13. 如何從班級訪問其他班級的成員?
- 14. 如何從一個班級移動到其他班級android
- 15. NSNotificationCenter通知不被髮送/接收
- 16. 向其他用戶發送動態通知(動作3)
- 17. 如何通過FCM向其他移動設備發送通知?
- 18. 一個班級需要另一個班級,其他班級需要第一個班級。我怎麼做?
- 19. 添加班級並刪除其他人
- 20. 向另一個班級顯示班級
- 21. 通知另一班級的變化
- 22. 班級如何通知某人其數據已被更改?
- 23. 如何與其他班級的表單控件進行通信?
- 24. 在活動上使用其他班級數據
- 25. 觸摸的NSNotificationCenter通知
- 26. NSNotificationCenter重新執行通知
- 27. 從其他班的進度連續通知表格
- 28. 如何向android中的其他設備發送推送通知?
- 29. 我該如何向其他人發送網絡/桌面通知?
- 30. 如何在班級內使用班級?
我如何訪問userInfo在我的方法中被調用? –
我會用更多的代碼編輯我的答案。 – pasine
@notme - 如果您發佈並註冊相同的通知名稱,情況會不會更好? – 2011-07-24 22:30:04