質問

いることができるよう一つのインスタンスを使用 NSNotificationCenteraddObserverpostNotificationName ができない事だけます。

私は2行のコードを追加するオブザーバーと送信メッセージの2つの異なるクラス

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded:) name:@"Event" object:nil];

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

まる名称を設定し nil で微細な足がんの放送がどのように定義する通知名称のメッセージになります。

役に立ちましたか?

解決

すべての私のコードはそうのようなNSNotificationsの使用になります:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ScanCompleted" object:nil];

最初の通知、通知の第二の転記を登録している。

他のヒント

基本的には、実行の順序をどうするすべてです。あなたがaddObserver前postNotificationNameを実行している場合、これは持っている簡単な問題です。使用ブレークポイントとコードのステップ:)

あなたの最初のブレークポイントはここに停止する必要があります:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"ScanCompleted" object:nil];

そして、ここます:

[[NSNotificationCenter defaultCenter]postNotificationName:@"ScanCompleted" object:self];
また、必ずセレクタがオンにコロンを持っています。それのメソッドシグネチャがなるのでます:

- (void)updateView:(NSNotification *)notification;

私は同じ問題を抱えていました。 その理由は、私がremoveObserver法と呼ばということです。

- (void)viewDidDisappear:(BOOL)animated{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

   [notificationCenter removeObserver:self];

}

ですから、postNotification前removeObserverと呼ばれたかのかどうかを確認します。

ヒント:あなたはこの関数を呼び出した場合は検索したキーワード「removeObserver」を検索することができます。

変更この:

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
これを

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:nil];
あなたの最初の通知が正しく登録されている場合は、

、newEventLoadedを呼び出す必要があります。

私は同様の問題を持っていたし、私の問題は、別のスレッドで呼び出された通知によるものでした。これは私の問題を解決しました。

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
});

また釣りをしたその他の名前が@"イベント"およびゼロ?んきるイベント名でファイルを含めるとともに通知を登録-送信.例えば:

ヘッダファイル:

extern NSString * const NOTE_myEventName;

ソースファイル:

NSString * const NOTE_myEventName = @"MyEventName";

参加登録:

[[NSNotificationCenter defaultCenter]
 addObserver:self
    selector:@selector(handleMyEvent:)
        name:NOTE_myEventName
      object:nil];

通知の送信:

[[NSNotificationCenter defaultCenter]
    postNotificationName:NOTE_myEventName object:nil];

I首尾固定私のクラッシュ "NSNotificationが呼ばれたときpostNotificationName:が送信されていない"。

私は本当のバグが通知メッセージハンドラである見つけました。

postNotificationNameaddObserverは、このスレッドの最初の投稿など、すべての権利です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top