Cocoa touch NSN通知导致分段错误

Cocoa touch NSN通知导致分段错误,cocoa-touch,ipad,exc-bad-access,nsnotification,Cocoa Touch,Ipad,Exc Bad Access,Nsnotification,我对NSNotification对象有一种奇怪的行为。 我的应用程序有一个导航控制器,第一个视图是表视图,第二个视图只是一个视图控制器,显示选定单元格的数据。 所以在这个数据视图控制器中,当我按下一个按钮时,我会发送一个通知。 通知一开始也有效 但是,当我返回到表视图并再次按下堆栈上的data view控制器并触摸带有通知的按钮时,整个应用程序崩溃,没有错误日志。 Xcode仅高亮显示这一行: [[NSNotificationCenter defaultCenter] postNotific

我对NSNotification对象有一种奇怪的行为。
我的应用程序有一个导航控制器,第一个视图是表视图,第二个视图只是一个视图控制器,显示选定单元格的数据。
所以在这个数据视图控制器中,当我按下一个按钮时,我会发送一个通知。 通知一开始也有效

但是,当我返回到表视图并再次按下堆栈上的data view控制器并触摸带有通知的按钮时,整个应用程序崩溃,没有错误日志。
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

卸载视图时,不要忘记删除观察者。基本上,当你向一个不存在的视图发布通知时,它无法运行选择器,从而使你的应用程序崩溃

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

根据您的描述,似乎每次推送视图控制器时,您都在创建视图控制器的新实例来执行此操作

如果是这种情况,您需要首先确保在返回表视图时没有泄漏视图控制器

然后,在该对象的dealloc方法中,从通知中取消订阅它

-(void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     //other deallocation code
     [super dealloc];
}