Swift NSDatePicker Sample Code

Swift NSDatePicker Sample Code

Created By:Debasis Das (10-Feb-2016)

In this post we will create a sample app to display a graphical NSDatePicker using a panel.

On selecting the value in the date picker, a text field will get updated with the selected date value and the panel will close.

We have used Delegate Design Pattern to propagate the selected value from the date picker to the date text field.

Swift NSDatePicker Sample code

Swift NSDatePicker Sample code

Swift NSDatePicker Sample Code

//  AppDelegate.swift
//  NSDatePickerSampleCode
//  Created by Debasis Das on 09/02/16.
//  Copyright © 2016 Knowstack. All rights reserved.

import Cocoa

@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,DatePickerProtocol {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var dateFieldForDelegate:NSTextField!
  
    var datePickerWindowController:NSDatePickerWindowController!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.window.delegate = self
    }

    func applicationWillTerminate(aNotification: NSNotification) {
    }

    
    @IBAction func datePickerUsingDelegatePattern(sender:AnyObject){
        let windowController = NSDatePickerWindowController(windowNibName: "NSDatePickerWindowController")
        self.datePickerWindowController = windowController
        self.datePickerWindowController.delegate = self //Here we are setting the App Delegate as the delegate to the DatePickerWindowController
        self.window.beginSheet(self.datePickerWindowController.window!, completionHandler: nil) //Here we are opening the DatePickerWindow as a sheet
    }
    
//The below method is a NSWindowDelegate method that we have overridden to render/show the date picker at a specific location. In our case just below the date text field
     func window(window: NSWindow, willPositionSheet sheet: NSWindow, usingRect rect: NSRect) -> NSRect{
        if sheet == self.datePickerWindowController.window
        {
            var rectToReturn = self.dateFieldForDelegate.frame
            rectToReturn.origin.y = rectToReturn.origin.y + 10.0
            return rectToReturn
        }
        else
        {
            return rect
        }
    }

    func selectedDate(date: NSDate) {
       //Here we are setting the date text view value with the date picker selected value
        self.dateFieldForDelegate.stringValue = String(date)
    }
    
}
//  NSDatePickerWindowController.swift
//  NSDatePickerSampleCode
//  Created by Debasis Das on 09/02/16.
//  Copyright © 2016 Knowstack. All rights reserved.

import Cocoa
//Here we are defining a Date Picker protocol that caller view controllers or window controller will implement.

@objc protocol DatePickerProtocol:class{
    func selectedDate(date:NSDate)
}

class NSDatePickerWindowController: NSWindowController {
    @IBOutlet weak var datePicker:NSDatePicker!
    weak var delegate:DatePickerProtocol! //The delegate of this window controller needs to be a class which implements the DatePickerProtocol
    
    override func windowDidLoad() {
        super.windowDidLoad()
        self.datePicker.dateValue = NSDate() //Here we are setting the default date picker value to today's date
    }
    
    @IBAction func selectDate(sender:AnyObject){
        
        self.delegate.selectedDate(self.datePicker.dateValue)
        self.window?.close()
    }
}

You can download the sample code here

Also read the Swift NSDatePicker using beginSheet and Completion Handler approach. In this we have removed the delegate approach and relying on completion handler of the beginSheet method of the window . Click here to read approach 2

Posted in Swift Tagged with: , ,
0 comments on “Swift NSDatePicker Sample Code
1 Pings/Trackbacks for "Swift NSDatePicker Sample Code"
  1. […] The date picker implementation using the delegate design pattern can be found here http://www.knowstack.com/swift-nsdatepicker-sample-code/ […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news