a NSTableView uses MyTableRowView (a custom NSView subclass) as content:
- (NSView *)tableView:(NSTableView *)tb viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
...
MyTableRowView *rowView = [[MyTableRowView alloc] init];
rowView.delegate = self.RowHandler;
...
return rowView;
}
Implementation of MyTableRowView:
// MyTableRowView.h
...
@property (nonatomic, unsafe_unretained) NSObject <MyTableRowViewDelegate> *delegate;
// MyTableRowView.m
...
- (void)mouseEntered:(NSEvent *)theEvent {
if ([self.delegate respondsToSelector:@selector(mouseEnteredRow:)])
[self.delegate mouseEnteredRow:self];
[[self nextResponder] mouseEntered:theEvent];
}
Problem 1: The TableView is used in Windows that displayed with [NSApp beginSheet:...]. When the Sheet is dismissed it is animated out of view. When the mouse cursor is over an MyTableRowView during this animation [MyTableRowView mouseEntered:] still gets called.
It seems that the NSTableView (and thus the "RowHandler" that is used as delegate for the MyTableRowViews) are already deallocated at this time. Thus the app crashes with EXC_BAD_ACCESSS when MyTableRowView tries to access its delegate.
I do not understand why MyTableRowView can still function while the table has been deallocated but that is the way it is.
I solved this problem by defining the delegate as weak instead of unsafe_unretained. This way the reference is set to NIL and can still be used although the object has been deallocated.
It this the right way to do this?
Problem 2: The app does not crash any more when accessing the delegate but when accessing the nextResponder. Calling the nextResponder is necessary for correct click/drag&drop handling.
As far as I know there is no way to tell if an object has been deallocates. So how can I check if I can use the nextResponder or not?