4

我只是在表单中添加一个 NSDatePicker,设置样式图形,然后设置如下操作:

[datePicker setAction:@selector(datePickSelected:)];

在该方法中,它只是打印出选定的日期。

-(void)datePickSelected:(id)sender
{
    NSLog(@"%@",[datePicker dateValue]);
}

它可以工作,但是当您单击此日期选择器中的日期时会运行两次。为什么是这样?

2011-05-25 15:17:09.382 xxx[6609:a0f] 2011-05-13 15:17:04 +0800
2011-05-25 15:17:09.677 xxx[6609:a0f] 2011-05-13 15:17:04 +0800
4

3 回答 3

3

这样做可以解决这个问题:

- (void) awakeFromNib {
  [datePickerControl sendActionOn:NSLeftMouseDown];  
}
于 2012-06-13T07:50:17.163 回答
2

The first answer doesn't work for me. I use this now in Swift:

override func awakeFromNib() {
    self.datePicker.target = self
    self.datePicker.action = Selector("dateSelected:")
    let action = Int(NSEventMask.MouseExitedMask.rawValue)
    self.datePicker.sendActionOn(action)
}

(But it's a little bit strange, the action is now called on mouseDown instead of mouseUp, but that doesn't matter for me now...)

于 2015-05-01T22:02:04.680 回答
2

这是一个带有内部的Swift 4解决方案:NSDatePickerNSViewController

class DatePickerVC: NSViewController{

  @IBOutlet weak var datePicker: NSDatePicker!
  var date:Date!

  override func viewDidLoad() {
    super.viewDidLoad()

    self.datePicker.target = self
    self.datePicker.action = #selector(dateSelected)

    let action = NSEvent.EventTypeMask.mouseExited
    self.datePicker.sendAction(on: action)
  }

  @objc func dateSelected(){
    print(datePicker.dateValue)
  }
}
于 2017-11-09T21:53:37.280 回答