Action is called twice (mouse up/down) when Target is clicked in Cocoa/objective-c

心不动则不痛 提交于 2019-12-11 13:27:10

问题


I have a NSDatePicker (target), and datePickerAction (action)

- (IBAction)datePickerAction:(id)sender
{

    if( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & 
       NSShiftKeyMask )
        NSLog(@"shift pressed %@", [datePicker dateValue]);

    else
        NSLog(@"hello %@", [datePicker dateValue]);

}

It works well as when I click a date in NSDatePicker object, the method(action) is called, the problem is that the method is called twice - mouse down/up.

Can I make the method called only once (up or down)?

EDIT

I only have the method to be selected.

And this is connection inspector.


回答1:


Unfortunately, I think you cannot set NSDatePicker (or NSDatePickerCell, to be specific) up this way in Interface Builder, but instead have to do it programmatically.

Here's a workaround that performs fine for me:

- (void)awakeFromNib
{
    // Assuming @property (assign) IBOutlet NSDatePickerCell *pickerCell;
    [self.pickerCell sendActionOn:NSLeftMouseDown]; // or NSLeftMouseUp or what have you...
}

Note that you cannot use NSLeftMouseDownMask here! (Well, of cause you can...but it won't help.)




回答2:


You typically want to add a target and action for a particular event. When you are making a connection using IB, look at the Connections Inspector. There is a list of control events to which you can set a target-action pair. Choose an event and drag to an action method.



来源:https://stackoverflow.com/questions/5303076/action-is-called-twice-mouse-up-down-when-target-is-clicked-in-cocoa-objective

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!