当前位置: 首页 > 工具软件 > cheddar-ios > 使用案例 >

iOS UIDatePicker

羿季
2023-12-01

In this tutorial, we’ll be discussing the UIDatePicker class and implement its various use cases in our iOS Application.

在本教程中,我们将讨论UIDatePicker类,并在我们的iOS应用程序中实现其各种用例。

UIDatePicker (UIDatePicker)

UIDatePicker class is a subclass of the UIPickerView. UIDatePicker control is used to display and select date and time values from the PickerView.

UIDatePicker类是UIPickerView的子类。 UIDatePicker控件用于显示和选择PickerView中的日期和时间值。

A UIDatePicker consists of multiple components for the date, time, seconds etc.
We can specify the timeInterval between consecutive rows.

UIDatePicker由日期,时间,秒等的多个组件组成。
我们可以指定连续行之间的timeInterval。

Following are the four major modes of UIDatePicker:

以下是UIDatePicker的四种主要模式:

  • date

    日期
  • dateAndTime – default

    dateAndTime –默认
  • time

    时间
  • countDownTimer

    倒计时器

We can set the mode on the UIDatePicker either from the Interface Builder or by using the property datePickerMode.

我们可以通过Interface Builder或使用属性datePickerMode在UIDatePicker上设置模式。

We can also configure the locale, timezone.
The date property represents the currently selected date in the UIDatePicker.

我们还可以配置语言环境,时区。
date属性表示UIDatePicker中当前选择的日期。

We can set the minimumDate and maximumDate properties from the interface builder or by using the equivalent properties in the Swift class.

我们可以通过界面构建​​器或使用Swift类中的等效属性来设置minimumDatemaximumDate属性。

Using the NSDateFormatter or NSCalendar class we can format the date returned from the UIDatePicker.

使用NSDateFormatterNSCalendar类,我们可以格式化从UIDatePicker返回的日期。

minuteInterval property is used to set the time interval.

minuteInterval属性用于设置时间间隔。

We can create an IBAction in the ViewController from the Interface Builder. The IBAction method would get triggered when an event occurs in the UIDatePicker. The event is generally valueChanged:

我们可以从Interface Builder在ViewController中创建IBAction。 当UIDatePicker中发生事件时,将触发IBAction方法。 该事件通常为valueChanged:

@IBAction func doSomething(sender: UIDatePicker, forEvent event: .valueChanged)

To connect it from the Interface Builder to the Swift class, we simply Ctrl + drag and drop. It opens a dialog like this:

要将其从Interface Builder连接到Swift类,我们只需Ctrl +拖放即可。 这样会打开一个对话框:

The UIDatePickerView can show dates beyond the minimumDate specified. BUT once you leave the touch control, the UIDatePickerView would slide back to the minimumDate.

UIDatePickerView可以显示超出指定的minimumDate日期。 但是,一旦离开触摸控件,UIDatePickerView就会滑回到最小日期。

In order to change the background of the UIDatePickerView, we simply use the backgroundColor property.

为了更改UIDatePickerView的背景,我们仅使用backgroundColor属性。

There are no public properties for the textColor. But we can always change them using there key.

textColor没有公共属性。 但是我们总是可以使用那里的键来更改它们。

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

In order to disable the current highlighted date, we can set the following attribute to false:

为了禁用当前突出显示的日期,我们可以将以下属性设置为false:

datePicker.setValue(false, forKey: "highlightsToday")

In the following section, we’ll create the UIDatePickerView in two ways and format the dates to be displayed in the UILabel and UITextField

在下一节中,我们将以两种方式创建UIDatePickerView并设置要在UILabelUITextField显示的日期的格式。

项目情节提要 (Project Storyboard)

(Code)

The code for the ViewController.swift file is given below:

下面给出了ViewController.swift文件的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myDatePicker: UIDatePicker!
    @IBOutlet weak var labelTwo: UILabel!
    @IBOutlet weak var labelOne: UILabel!
    @IBOutlet weak var myTextField: UITextField!
    
    let datePicker = UIDatePicker()
    @IBAction func dateMode(_ sender: Any) {
        
        datePicker.datePickerMode =  .date
    }
    
    @IBAction func timeMode(_ sender: Any) {
    datePicker.datePickerMode =  .time
    
    }
    
    
    @IBAction func countdownMode(_ sender: Any) {
        datePicker.datePickerMode =  .countDownTimer
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myDatePicker.addTarget(self, action: #selector(ViewController.datePickerValueChanged(_:)), for: .valueChanged)
        createDatePicker()
        createToolbar()
    }
    
    func createDatePicker()
    {
        
        datePicker.datePickerMode = .dateAndTime
        datePicker.backgroundColor = .white
        datePicker.minuteInterval = 30
        datePicker.setValue(UIColor.brown, forKey: "textColor")
        myTextField.inputView = datePicker
        
    }
    
    func createToolbar()
    {
        let toolBar = UIToolbar()
        toolBar.barStyle = .default
        toolBar.isTranslucent = true
        toolBar.sizeToFit()
        
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneClick))
        toolBar.setItems([doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        myTextField.inputAccessoryView = toolBar
        
    }
    
    @objc func doneClick() {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .medium
        dateFormatter.timeStyle = .medium
        myTextField.text = dateFormatter.string(from: datePicker.date)
        myTextField.resignFirstResponder()
    }
    
    @objc func datePickerValueChanged(_ sender: UIDatePicker){
       
        let dateFormatter: DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yyyy hh:mm a"
        let selectedDate: String = dateFormatter.string(from: sender.date)
        
        labelOne.text = selectedDate
    
        let components = Calendar.current.dateComponents([.year, .month, .day], from: sender.date)
        if let day = components.day, let month = components.month, let year = components.year {
            labelTwo.text = "Day: \(day) Month: \(month) Year: \(year)"
        }
    
    }


}

In the above code, we’ve created a UIDatePicker in the Storyboard, and another programmatically.
The programmatically one displays the date formatted in the UITextField.

在上面的代码中,我们在情节提要中创建了一个UIDatePicker,并以编程方式创建了另一个。
以编程方式显示UITextField中格式化的日期。

The UIDatePicker from the Storyboard displays the date using NSDateFormatter and Calendar classes in each of the UILabel.
We’ve set the minuteInterval to 30 minutes.

情节提要中的UIDatePicker使用每个UILabel NSDateFormatterCalendar类显示日期。
我们将分钟minuteInterval设置为30分钟。

Instead of creating an IBAction, we’ve done the same programmatically using addTarget

除了使用IBAction之外,我们还使用addTarget以编程方式进行了相同的操作

The output of the application in action is :

实际应用程序的输出为:

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

翻译自: https://www.journaldev.com/22784/ios-uidatepicker

 类似资料:

相关阅读

相关文章

相关问答