Swift-Usage of the Date selection control (Uidatepicker) basics

Source: Internet
Author: User
Tags dateformat locale uikit
1, use the storyboard to create a date selection control
First we add a UIDatePicker control and a button directly to the Main.Storyboard. This button is used to pop up a prompt box to display the currently selected date and time.
At the same time, use IBOutlet in ViewController.swift to establish the association between the control and the event. The specific code is as follows:
Import UIKit
 
Class ViewController: UIViewController {
     
    @IBOutlet var dpicker: UIDatePicker!
    @IBOutlet var btnshow:UIButton!
     
    Override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    @IBAction func showClicked(_ sender:UIButton)
    {
        Let date = dpicker.date
         
        // Create a date formatter
        Let dformatter = DateFormatter()
        // Set the format string for the date formatter
        dformatter.dateFormat = "yyyy year MM month dd day HH:mm:ss"
        // format date and time using date formatter
        Let datestr = dformatter.string(from: date)
         
        Let message = "The date and time you selected is: \(datestr)"
         
        // Create a UIAlertController object (message box) and display the date and time selected by the user through the message box
        Let alertController = UIAlertController(title: "current date and time",
                                                Message: message,
                                                preferredStyle: .alert)
        Let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)
        Self.present(alertController, animated: true, completion: nil)
    }
}

Original from: www.hangge.com Reprinted please keep the original link: https://www.hangge.com/blog/cache/detail_547.html


2, pure code creation date selection control


Import UIKit
 
Class ViewController: UIViewController {
     
    Override func viewDidLoad() {
        super.viewDidLoad()
         
        / / Create a date selector
        Let datePicker = UIDatePicker(frame: CGRect(x:0, y:0, width:320, height:216))
        / / Set the date selector area to Chinese, the date of the selector is displayed in Chinese
        datePicker.locale = Locale(identifier: "zh_CN")
        / / Note: the method name inside the action needs to add a colon ":"
        datePicker.addTarget(self, action: #selector(dateChanged),
                             For: .valueChanged)
        self.view.addSubview(datePicker)
    }
     
    / / Date selector response method
    Func dateChanged(datePicker : UIDatePicker){
        / / Update reminder time text box
        Let formatter = DateFormatter()
        //date style
        formatter.dateFormat = "yyyy year MM month dd day HH:mm:ss"
        Print(formatter.string(from: datePicker.date))
    }
     
    Override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}




3, date selection control text changed to Chinese
The text in the default date selection control is English. If you want to display Chinese, you need to set the area of the date selection control as follows.


/ / Set the date selector area to Chinese, the date of the selector is displayed in Chinese
datePicker.locale = Locale(identifier: "zh_CN")


4, change the control time selection mode
By default, the options in the date selection control have both date and time. We can modify the selection mode so that it has only one of them.
(1) Only date selection

datePicker.datePickerMode = .date


(2) only time selection


datePicker.datePickerMode = .time


5, modify the text color
UIDatePicker The default text color is black, but if we use a dark background (for example) it will be illegible. We can modify the text color by setValue.
For example, we change the text to white:

	
datePicker.setValue(UIColor.white, forKey: "textColor")





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.