gpt4 book ai didi

ios - 带有 "done"按钮的 UIDatePicker

转载 作者:技术小花猫 更新时间:2023-10-29 10:56:44 25 4
gpt4 key购买 nike

我需要向我的日期选择器添加一个“完成”按钮,如下图所示:

enter image description here

这是我的代码:

-(IBAction)chooseDate:(id)sender{
self.datepicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.datepicker.datePickerMode = UIDatePickerModeDate;
datepicker.backgroundColor = Rgb2UIColor(52, 170, 220);

[self.btnDone
addTarget:self
action:@selector(SetDatePickerTime:)
forControlEvents:UIControlEventTouchDown];

[self.view addSubview:self.datepicker];
}

最佳答案

用 swift

// Place this code inside your ViewController or where you want ;)

var txtField: UITextField = UITextField(frame: CGRectMake(0, 0, 200, 50))

// the formatter should be "compliant" to the UIDatePickerMode selected below
var dateFormatter: NSDateFormatter {
let formatter = NSDateFormatter()
// customize the locale for the formatter if you want
//formatter.locale = NSLocale(localeIdentifier: "it_IT")
formatter.dateFormat = "dd/MM/yyyy"
return formatter
}

override func viewDidLoad() {
super.viewDidLoad()

// date picker setup
let datePickerView:UIDatePicker = UIDatePicker()

// choose the mode you want
// other options are: DateAndTime, Time, CountDownTimer
datePickerView.datePickerMode = UIDatePickerMode.Date

// choose your locale or leave the default (system)
//datePickerView.locale = NSLocale.init(localeIdentifier: "it_IT")

datePickerView.addTarget(self, action: "onDatePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
txtField.inputView = datePickerView

// datepicker toolbar setup
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "doneDatePickerPressed")

// if you remove the space element, the "done" button will be left aligned
// you can add more items if you want
toolBar.setItems([space, doneButton], animated: false)
toolBar.userInteractionEnabled = true
toolBar.sizeToFit()

txtField.inputAccessoryView = toolBar

self.view.addSubview(txtField)
}

func doneDatePickerPressed(){
self.view.endEditing(true)
}

func onDatePickerValueChanged(datePicker: UIDatePicker) {
self.txtField = dateFormatter.stringFromDate(datePicker.date)
}

关于ios - 带有 "done"按钮的 UIDatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com