DatePicker config quickly

Sorry for my english and question!

I am using xCode 6 with swift

I'm trying to develop my own application and I don't know how to ask the user to set a date, so I can use that value on the other side: S, I don't care if it uses a DatePicker with scrolling or some other way, it just needs the user to be able to set any date. I cannot guess how datePicker: S works

I found this code inside the course that develops "Tinder", when it asks for your date of birth in the login, I adapt it for my application, but it doesn't work and I don't 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)
    })
}

      

+3


source to share


1 answer


I have made some changes for you. In addition to adding one right parenthesis at the end of the code, I have changed the CGRectMake coordinates in two places. Everything was on the screen. When I implemented the code, I added a button to display the date in the upper left corner of the view and linked it to the IBOutlet. This should get you off to 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)
        })
    }
}

      

+5


source







All Articles