単一のオブジェクトの複数のイベントについて複数のオブザーバーが必要です(1対Nの関係)。
このタスクを実行するメカニズムは、によって提供されますNSNotificationCenter
。私の問題に使用すると、メカニズムはかなりやり過ぎに見えます。
を使用せずに手動で行う方法NSNotificationCenter
:
- (void)addDelegate:(id<DelegateProtocol>)delegate;
- (void)removeDelegate:(id<DelegateProtocol>)delegate;
オブジェクトにオブザーバーを追加および削除します。
- (void)someEventFired:(NSObject<NSCopying> *)eventData
{
for (id delegate in delegates) {
NSObject *data = [eventData copy];
[delegate someEventFired:data];
}
}
このメカニズムは、オブジェクトが追加の文字列を共有する必要がなく、簡単に実装できます。
- iOSフレームワークに1対Nのデリゲート(C#イベントなど)の公式パターンはあり
NSNotificationCenter
ますか? - いつ使用すべき
NSNotificationCenter
か、いつ使用すべきでないか? - ここで提案しているような実装をいつ使用する必要がありますか?