notification being posted once but method being called twice

Discussion in 'Mac Programming' started by Chirone, Apr 21, 2009.

  1. Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    it feels like i'm missing something

    in the class that listens for an event i just did this:

    Code:
    NSNotificationCenter* nc;
    		nc = [NSNotificationCenter defaultCenter];
    		[nc addObserver: self
    			   selector: @selector(handleViewChange:)
    				   name: [Constants viewChangeNote]
    				 object: nil];
    where Constants is a custom class
    so that makes the class listen for the notification

    the method that posts the notification is like this:
    printf("right\n");

    NSMutableDictionary* info = [NSMutableDictionary dictionary];
    [info setValue: [NSValue valueWithPoint: dragStartPoint] forKey: [Constants dragPointStart]];
    [info setValue: [NSValue valueWithPoint: [event locationInWindow]] forKey: [Constants dragPointEnd]];

    NSNotificationCenter* nc;
    nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName: [Constants viewChangeNote]
    object: self
    userInfo: info];
    this happens when the user lets go of the right mouse button

    now in the console "right" is printed out once
    but straight after that the method that is assigned to that event (handleChangeView) happens twice
    the statements that it prints out print out twice

    any idea what i'm missing? :confused:
     
  2. Chirone thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #2
    nevermind...

    found the solution

    i'm awakeFromNib is called twice, which meant it was being added as an observer to the same event twice, so i just had to put the adding observer code to the init method....
     

Share This Page