0

I have instantiated and initialized a subclass of NSDatePicker in AppDelegate -> applicationDidFinishLaunching and overrode mouseDown(with event: NSEvent)in the datePicker subclass. When I press the mouse button in the datePicker and break in its overridden mouseDown func the instance is not the one I instantiated in applicationDidFinishLaunching and so is not initialized.

I've tried creating the instance at different entry points thinking it might be a timing thing but I've gotten nowhere. I'm out of ideas and feeling a little feeble. Any Help?

The datePicker:

import Cocoa

class AlarmIVDatePicker: NSDatePicker {
    var viewController: ViewController!

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
    }
    override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
        return true
    }
    override func mouseDown(with event: NSEvent) {
        let stop = 0
    }
}

The ViewController:

class ViewController: NSViewController, NSWindowDelegate{

var alarmIVDatePicker: AlarmIVDatePicker!


override func viewDidLoad() {
    super.viewDidLoad()

    alarmIVDatePicker = AlarmIVDatePicker()
    alarmIVDatePicker.viewController = self


}

I expected I could access the values I had set but the instance is not the one I created and all the values are nil

mswade
  • 5
  • 5
  • Instantiating a date picker in an AppDelegate is weird. Why not instantiate it in the `viewDidLoad` or `viewWillAppear` of whatever view or window controller that actually uses it? Also, are you using storyboards or xib files and are you using an `IBOutlet` for that datepicker or how do you reference the date picker in your view? – Michael Dautermann Nov 05 '19 at 20:14
  • Why in the AppDelegate I have no good answer but agree and so now instantiate it in viewDidLoad: override func viewDidLoad() { super.viewDidLoad() alarmIVDatePicker = AlarmIVDatePicker() alarmIVDatePicker.viewController = self } and referencing it using an IBOutlet: @IBOutlet var alarmIVDatePicker: AlarmIVDatePicker! It isn't the same instance after the mouseDown and its viewController is nil. – mswade Nov 06 '19 at 00:22
  • You only need to use an `IBOutlet` if you connected that outlet to your subclassed `AlarmIVDatePicker` living in a storyboard or XIB file. Are you using either for the view where that picker lives? – Michael Dautermann Nov 06 '19 at 00:24
  • Doesn't matter how I reference the datepicker, with an IBOutlet or declare a class variable. Just thinking I haven't mentioned the datepicker is textual with stepper, don't see what difference that makes – mswade Nov 06 '19 at 00:34

1 Answers1

0

Okay. Here's what you can do to track this down. Add this code to your AlarmIVDatePicker class:

override init(frame frameRect: NSRect) {
    Swift.print("AlarmIVDatePicker being called here")
    super.init(frame: frameRect)
}

convenience init() {
    self.init(frame: .zero)
}

required init?(coder: NSCoder) {
    Swift.print("hey, you DID drop this into a storyboard or XIB file!")
    super.init(coder: coder)
}

If you set breakpoints inside these init methods, you'll catch when and where they are being called where you did not expect.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Okay, thanks, so feeling a bit sheepish; the datepicker was added to the view with a storyboard and referenced with an IBOutlet yet I stepped all over that by assigning a new instance to that variable in viewDidLoad. Stepping through the inits you provided set me straight, thanks – mswade Nov 06 '19 at 01:17
  • no worries I'm really happy you got it figured out! – Michael Dautermann Nov 06 '19 at 01:42