0
votes

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

1
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
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
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
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 makesmswade

1 Answers

0
votes

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.