Swift NSDatePicker Sample Code 2 Using beginSheet

Swift NSDatePicker Sample Code 2 Using beginSheet

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

This is a continuation to the previous post where we created a NSDatePicker window controller and used delegate pattern to translate the selected date value to a date text field in the main view/window

In this post we will use the beginSheet completionHandler to get the selected value and will create an alternate approach from the delegate design pattern

The date picker implementation using the delegate design pattern can be found here http://www.knowstack.com/swift-nsdatepicker-sample-code/

NSDatePicker beginSheet completionHandler

NSDatePicker beginSheet completionHandler

Sample Code for NSDatePicker using a Window Controller and beginSheet with completion handler

//  AppDelegate.swift
//  NSDatePicker Sample 2
//  Created by Debasis Das on 12/02/16.
//  Copyright © 2016 Knowstack. All rights reserved.

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

    @IBOutlet weak var window: NSWindow!
    var datePickerWindowController:KSDatePickerWindowController!
    @IBOutlet weak var dateTextField:NSTextField!

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

    @IBAction func showDatePicker(sender:AnyObject){
        self.datePickerWindowController =  KSDatePickerWindowController(windowNibName: "DatePickerWindow")
        self.window!.beginSheet((self.datePickerWindowController.window)!, completionHandler: {responseCode  in
            if (responseCode == NSModalResponseOK)
            {
                print("NSModalResponseOK")
                self.dateTextField.stringValue = String(self.datePickerWindowController.selectedDate)
            }
            else if (responseCode == NSModalResponseCancel)
            {
                print("NSModalResponseCancel")
            }
            else
            {
                NSLog("Other")
            }
        })
    }
    
    func window(window: NSWindow, willPositionSheet sheet: NSWindow, usingRect rect: NSRect) -> NSRect{
        if sheet == self.datePickerWindowController.window
        {
            var rectToReturn = self.dateTextField.frame
            rectToReturn.origin.y = rectToReturn.origin.y + 10.0
            return rectToReturn
        }
        else
        {
            return rect
        }
    }

}

class KSDatePickerWindowController:NSWindowController{
    @IBOutlet weak var datePicker:NSDatePicker!
    var selectedDate:NSDate!
    
    override func windowDidLoad() {
        super.windowDidLoad()
        self.datePicker.dateValue = NSDate()
        self.datePicker.backgroundColor = NSColor.yellowColor()
    }
    
    @IBAction func selectDate(sender:AnyObject){
        self.selectedDate = self.datePicker.dateValue
        self.window!.sheetParent?.endSheet(self.window!, returnCode: NSModalResponseOK)
        self.window!.close()
    }
    
    @IBAction func closeDatePicker(sender:AnyObject){
        self.window!.sheetParent?.endSheet(self.window!, returnCode: NSModalResponseCancel)
        self.window!.close()

    }
}

You can download the source code here NSDatePicker Sample 2

Posted in Swift Tagged with: , , ,
0 comments on “Swift NSDatePicker Sample Code 2 Using beginSheet
1 Pings/Trackbacks for "Swift NSDatePicker Sample Code 2 Using beginSheet"
  1. […] 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 […]

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