6

NSNotificationオブジェクトで奇妙な動作をします。
私のアプリにはナビゲーションコントローラーがあり、最初のビューはテーブルビューで、2番目のビューは選択したセルのデータを表示するビューコントローラーです。
したがって、このデータビューコントローラーでは、ボタンを押すと通知を送信します。通知も最初は機能します。

しかし、テーブルビューに戻り、スタック上のデータビューコントローラーをもう一度押して、通知のあるボタンをタッチすると、アプリ全体がエラーログなしでクラッシュします。
Xcodeはこの行のみを強調表示します:

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

通知を送信する関数:

- (IBAction) toggleNoteView: (id) sender
{
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"toggleNoteView" object:nil];
}

これは受信者です:

- (id)init {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(toggleNoteView:) 
                                             name:@"toggleNoteView" object:nil];
     ...
}

- (void) toggleNoteView:(NSNotification *)notif  {

    takingNotes = !takingNotes;
}

編集:今、私はいくつかのエラーログを取得しました。

2011-06-27 23:05:05.957 L3T[3228:707] -[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0
2011-06-27 23:05:06.075 L3T[3228:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0'
*** Call stack at first throw:
(
0   CoreFoundation                      0x3634f64f __exceptionPreprocess + 114
1   libobjc.A.dylib                     0x370a2c5d objc_exception_throw + 24
2   CoreFoundation                      0x363531bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3   CoreFoundation                      0x36352649 ___forwarding___ + 508
4   CoreFoundation                      0x362c9180 _CF_forwarding_prep_0 + 48
5   Foundation                          0x35c45183 _nsnote_callback + 142
6   CoreFoundation                      0x3631e20f __CFXNotificationPost_old + 402
7   CoreFoundation                      0x362b8eeb _CFXNotificationPostNotification + 118
8   Foundation                          0x35c425d3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 70
9   Foundation                          0x35c441c1 -[NSNotificationCenter postNotificationName:object:] + 24
10  L3T                                 0x0003d17f -[Container toggleNoteView:] + 338
11  CoreFoundation                      0x362bf571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
4

2 に答える 2

8

ビューをアンロードするときは、オブザーバーを削除することを忘れないでください。基本的に何が起こっているのかというと、存在しないビューに通知を投稿すると、セレクターを実行できなくなり、アプリがクラッシュします。

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}
于 2011-06-28T00:44:17.477 に答える
3

説明から、View Controllerをプッシュするたびに、それを行うためにViewControllerの新しいインスタンスを作成しているように見えます。

その場合は、最初に、テーブルビューに戻ったときに、そのViewControllerがリークしていないことを確認する必要があります。

次に、そのオブジェクトのdeallocメソッドで、通知のサブスクライブを解除します。

-(void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     //other deallocation code
     [super dealloc];
}
于 2011-06-28T00:46:48.337 に答える