可可触摸 – NSNotification导致分段错误

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

但是当我回到表格视图并再次将数据视图控制器推入堆栈并通过通知触摸按钮时,整个应用程序崩溃而没有错误日志.
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];
}
相关文章
相关标签/搜索