defaultCenter's addObserver

  • From MyDocument's windowControllerDidLoadNib I call a method in
    MyController, which in turn calls:

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(aSelector:) name:aName
    object:anotherControllerObject];

    The docs state that this passed object is the object that is
    encapsulated [NSNotification object] in the NSNotification* passed to
    the selector.

    This selector has a footprint =
        - (void) aSelector:(NSNotification*)sentNotification;

    I pass another Controller, iboAnotherCtrl, as this object:

    Later on I call within AnotherController:
        [[NSNotificationCenter defaultCenter] postNotificationName:aName
    object:self];

    .. and everything works.

    Now here's the sticky .. the docs state that if this encapsulated
    [NSNotification object] = nil, the observer is notified when *any*
    encapsulated object is sent to the selector.

    So just for whatever, I pass object:nil to
    addObserver:selector:name:object:.  Then, within my selector I test
    for the type of passed encapsulated object and something(?) doesn't
    work.  My selector gets called for my first new MyDocument.  But when
    I open up a second new MyDocument, the Debugger complains about an
    invalid aSelector.  I don't understand what's going on, so I'm looking
    for help.

    Again, everything works just great when I pass a non-nil object to
    defaultCenter's addObserver:selector:name:object:

    Thanks in advance,

    John Love
  • On Oct 7, 2008, at 8:26 AM, John Love wrote:

    > So just for whatever, I pass object:nil to
    > addObserver:selector:name:object:. Then, within my selector I test
    > for the type of passed encapsulated object and something(?) doesn't
    > work. My selector gets called for my first new MyDocument. But when
    > I open up a second new MyDocument, the Debugger complains about an
    > invalid aSelector. I don't understand what's going on, so I'm
    > looking for help.

    Please provide the exact error from the debugger. My guess would be
    that you have some sort of memory management error, or that you didn't
    provide the correct selector (Note that a ":" at the end of a method
    name is significant, and needs to be included in the selector).

    j o a r
  • On Oct 7, 2008, at 12:03 PM, John Love wrote:

    > 2008-10-07 14:36:14.149 Calculate Medical[265:10b] *** -[NSCFSet
    > calcStatusChanged:]: unrecognized selector sent to instance 0xe41f250

    John,

    This type of error message indicates that your notification observer
    has been deallocated. This would probably be because you forgot that
    you have to remove notification observers before they are deallocated.
    In many cases you would put your unsubscribe statement in your dealloc
    method.

    It could also indicate any other type of memory management error.
    NSZobieEnabled is the general facility for troubleshooting that type
    of problem, but in this case you could probably do with simple code
    inspection.

    j o a r
  • On Oct 7, 08, at 5:58 PM, j o a r wrote:

    >
    > On Oct 7, 2008, at 12:03 PM, John Love wrote:
    >
    >> 2008-10-07 14:36:14.149 Calculate Medical[265:10b] *** -[NSCFSet
    >> calcStatusChanged:]: unrecognized selector sent to instance 0xe41f250
    >
    >
    > John,
    >
    > This type of error message indicates that your notification observer
    > has been deallocated. This would probably be because you forgot that
    > you have to remove notification observers before they are
    > deallocated. In many cases you would put your unsubscribe statement
    > in your dealloc method.

    Worse than that .. when I passed an observed object = nil for -
    addObserver, I passed an observed object = non-nil to -removeObserver.

    > It could also indicate any other type of memory management error.
    > NSZobieEnabled is the general facility for troubleshooting that type
    > of problem, but in this case you could probably do with simple code
    > inspection.

    Thanks for introducing me to NSZobieEnabled -- love the name!

    Thanks bunches, joar

    John Love
    Touch the Future! Teach!
previous month october 2008 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today