Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePicker configuration swift

sorry for my english and question!

I´m using xCode 6 with swift

I´m trying to develop my own app, and i don´t know how to ask the user to set a date, so i can use that value on other side :S i don´t care if it uses a DatePicker with scroll or other way, just need that the user can set any date. I can´t guess how the datePicker works :S

I found this code inside a course that develops "Tinder", when it asks for your date of birth in the login, i adapt it to my app, but it doesnt work, and i dont know why :S

import UIKit

class SignUpController: UIViewController, UITextFieldDelegate {

var pickerContainer = UIView()
var picker = UIDatePicker()

@IBOutlet weak var selectedDate: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()


    self.selectedDate.setTitle("", forState: UIControlState.Normal)

    configurePicker()
}

func configurePicker()
{
    pickerContainer.frame = CGRectMake(0.0, 600.0, 320.0, 300.0)
    pickerContainer.backgroundColor = UIColor.whiteColor()

    picker.frame    = CGRectMake(0.0, 20.0, 320.0, 300.0)
    picker.setDate(NSDate(), animated: true)
    picker.maximumDate = NSDate()
    picker.datePickerMode = UIDatePickerMode.Date
    pickerContainer.addSubview(picker)

    var doneButton = UIButton()
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    doneButton.addTarget(self, action: Selector("dismissPicker"), forControlEvents: UIControlEvents.TouchUpInside)
    doneButton.frame    = CGRectMake(250.0, 5.0, 70.0, 37.0)

    pickerContainer.addSubview(doneButton)

    self.view.addSubview(pickerContainer)
}

@IBAction func selectDate(sender: AnyObject)
{



    UIView.animateWithDuration(0.4, animations: {

        var frame:CGRect = self.pickerContainer.frame
        frame.origin.y = self.view.frame.size.height - 300.0 + 84
        self.pickerContainer.frame = frame

    })
}

func dismissPicker ()
{
    UIView.animateWithDuration(0.4, animations: {

        self.pickerContainer.frame = CGRectMake(0.0, 600.0, 320.0, 300.0)

        let dateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = "dd/MM/yyyy"

        self.selectedDate.setTitle(dateFormatter.stringFromDate(self.picker.date), forState: UIControlState.Normal)
    })
}
like image 539
Mati Rocchetti Avatar asked Mar 16 '26 10:03

Mati Rocchetti


1 Answers

I made some changes for you. In addition to adding one right bracket at the end of the code, I modified the CGRectMake coordinates in two places. Things were off the screen. When I implemented the code I added the button for the date display in the upper left corner of the view and connected it to the IBOutlet. This should give you a good start.

import UIKit

class SignUpController: UIViewController, UITextFieldDelegate {

    var pickerContainer = UIView()
    var picker = UIDatePicker()

    @IBOutlet weak var selectedDate: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()


        self.selectedDate.setTitle("", forState: UIControlState.Normal)

        configurePicker()
    }

    func configurePicker()
    {
        pickerContainer.frame = CGRectMake(0.0, 50, 320.0, 300.0)
        pickerContainer.backgroundColor = UIColor.whiteColor()

        picker.frame    = CGRectMake(0.0, 20.0, 320.0, 300.0)
        picker.setDate(NSDate(), animated: true)
        picker.maximumDate = NSDate()
        picker.datePickerMode = UIDatePickerMode.Date
        pickerContainer.addSubview(picker)

        var doneButton = UIButton()
        doneButton.setTitle("Done", forState: UIControlState.Normal)
        doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        doneButton.addTarget(self, action: Selector("dismissPicker"), forControlEvents: UIControlEvents.TouchUpInside)
        doneButton.frame    = CGRectMake(250.0, 5.0, 70.0, 37.0)

        pickerContainer.addSubview(doneButton)

        self.view.addSubview(pickerContainer)
    }

    @IBAction func selectDate(sender: AnyObject)
    {



        UIView.animateWithDuration(0.4, animations: {

            var frame:CGRect = self.pickerContainer.frame
            frame.origin.y = self.view.frame.size.height - 300.0 + 84
            self.pickerContainer.frame = frame

        })
    }

    func dismissPicker ()
    {
        UIView.animateWithDuration(0.4, animations: {

            self.pickerContainer.frame = CGRectMake(0.0, 50, 320.0, 300.0)

            let dateFormatter = NSDateFormatter()

            dateFormatter.dateFormat = "dd/MM/yyyy"

            self.selectedDate.setTitle(dateFormatter.stringFromDate(self.picker.date), forState: UIControlState.Normal)
        })
    }
}
like image 120
Steve Rosenberg Avatar answered Mar 19 '26 00:03

Steve Rosenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!