Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Val K

macrumors member
Original poster
Dec 19, 2012
33
0
Italy
I have a custom class for NSButton called `MyButton` where I post a notification for a quicksave
`MyButton.m`:
Code:
    -(void)mouseDown:(id)sender{
        [super mouseDown:sender];
        [super mouseUp:sender];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"quickSave" object:nil userInfo:nil];
    }
in `AppDelegate` I get the notification for the quick save:
`AppDelegate.m`:
Code:
    - (IBAction)saveAction:(id)sender{
        NSLog(@"Saving...");
        NSError *error = nil;
        if (![[self managedObjectContext] commitEditing]) {
            NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
        }
        if (![[self managedObjectContext] save:&error]) {
            [[NSApplication sharedApplication] presentError:error];
        }
    }
    -(void)awakeFromNib{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveAction:) name:@"quickSave" object:nil];
    }
Via the `NSLog "Saving..."` I see that the saveAction is called 2 times. Why? Thanks in advance
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Via the `NSLog "Saving..."` I see that the saveAction is called 2 times. Why? Thanks in advance

Did you register more than one object for that notification?

In the NSLog statement, log sender and self as well.
 

Val K

macrumors member
Original poster
Dec 19, 2012
33
0
Italy
no, as you can see there's no specified object, it's "nil".
this is the log:
Code:
Saving...
sender: NSConcreteNotification 0x101915b70 {name = quickSave}
self: <AppDelegate: 0x101927730>
Saving...
sender: NSConcreteNotification 0x101915b70 {name = quickSave}
self: <AppDelegate: 0x1005374c0>
there are two different self. what does it mean?

PS: i've tried this solution but it doesn't work
Code:
-(void)awakeFromNib{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"quickSave" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveAction:) name:@"quickSave" object:nil];
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.