gpt4 book ai didi

ios - 填充两个 UITextField 的两个 UiDatePicker 之间的差异

转载 作者:行者123 更新时间:2023-11-30 13:55:49 25 4
gpt4 key购买 nike

我需要获取存储在两个 UITextField 中的两个 UIDatePicker 之间的差异(以天为单位)。

我在论坛上找到了这个,但它不起作用。

1. let inicio = inicioTextField.text
2. let fim = fimTextField.text

3. let dateFormatter = NSDateFormatter()
4. dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
5. let inicioDate: NSDate = dateFormatter.dateFromString(inicio)
6. let fimDate: NSDate = dateFormatter.dateFromString(fim)

7. let cal = NSCalendar.currentCalendar()
8. let unit = NSCalendarUnit.Day
9. let components = cal.components(unit, fromDate: inicioDate, toDate: fimDate, options: [])

我收到一条消息,要求添加!在第 5 行和第 6 行之后,但它不起作用,应用程序在启动之前崩溃。

将来我需要检查这个天数来设置很多案例。

这就是我的 UITextFild 的填充方式

@IBAction func textFieldEditing(sender: UITextField) {

let datePickerViewInicio: UIDatePicker = UIDatePicker()

datePickerViewInicio.datePickerMode = UIDatePickerMode.Date

sender.inputView = datePickerViewInicio

datePickerViewInicio.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

}

func datePickerValueChanged(sender: UIDatePicker) {

let dateFormatter = NSDateFormatter()

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle

inicioTextField.text = dateFormatter.stringFromDate(sender.date)

完整代码!

@IBOutlet weak var inicioTextField: UITextField!

@IBOutlet weak var fimTextField: UITextField!

@IBOutlet weak var motivoTextField: UITextField!

var motivos: [String] = [String]()

override func viewDidLoad()
{
super.viewDidLoad()

//Toolbar para data de Início

let motivoPicker: UIPickerView = UIPickerView()

motivos = ["Pedido de demissão","Dispensa sem justa causa","Dispensa com justa causa", "Término do contrato de experiência"]

motivoPicker.delegate = self
motivoPicker.dataSource = self
motivoTextField.inputView = motivoPicker

let toolBarIni = UIToolbar()
toolBarIni.sizeToFit()
toolBarIni.barStyle = UIBarStyle.Default
toolBarIni.tintColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)


let hojeBtnIni = UIBarButtonItem(title: "Hoje", style: UIBarButtonItemStyle.Plain, target: self, action: "tappedToolBarBtn:")

let okBarBtnIni = UIBarButtonItem(title: "OK", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed:")

let flexSpaceIni = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

let labelInicio = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))

labelInicio.font = UIFont(name: "Helvetica", size: 12)

labelInicio.backgroundColor = UIColor.clearColor()

labelInicio.textColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)

labelInicio.text = "Início do contrato"

labelInicio.textAlignment = NSTextAlignment.Center

let textBtnIni = UIBarButtonItem(customView: labelInicio)

toolBarIni.setItems([hojeBtnIni,flexSpaceIni,textBtnIni,flexSpaceIni,okBarBtnIni], animated: false)

inicioTextField.inputAccessoryView = toolBarIni

//toolbar para data do Fim


let toolBarFim = UIToolbar()

toolBarFim.sizeToFit()
toolBarFim.barStyle = UIBarStyle.Default
toolBarFim.tintColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)


let todayBtnFim = UIBarButtonItem(title: "Hoje", style: UIBarButtonItemStyle.Plain, target: self, action: "tappedToolBarBtnFim:")

let okBarBtnFim = UIBarButtonItem(title: "OK", style: UIBarButtonItemStyle.Done, target: self, action: "donePressedFim:")

let flexSpaceFim = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

let labelFim = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))

labelFim.font = UIFont(name: "Helvetica", size: 12)

labelFim.backgroundColor = UIColor.clearColor()

labelFim.textColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)

labelFim.text = "Fim do contrato"

labelFim.textAlignment = NSTextAlignment.Center

let textBtnFim = UIBarButtonItem(customView: labelFim)

toolBarFim.setItems([todayBtnFim,flexSpaceFim,textBtnFim,flexSpaceFim,okBarBtnFim], animated: true)

fimTextField.inputAccessoryView = toolBarFim

//Toolbar para Motivo

let toolBarMot = UIToolbar()
toolBarMot.sizeToFit()
toolBarMot.barStyle = UIBarStyle.Default
toolBarMot.tintColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)


//let hojeBtnMot = UIBarButtonItem(title: "Cancelar", style: UIBarButtonItemStyle.Plain, target: self, action: "tappedToolBarBtnMot:")

let okBarBtnMot = UIBarButtonItem(title: "OK", style: UIBarButtonItemStyle.Done, target: self, action: "donePressedMot:")

let flexSpaceMot = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

let labelMot = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))

labelMot.font = UIFont(name: "Helvetica", size: 11)

labelMot.backgroundColor = UIColor.clearColor()

labelMot.textColor = UIColor(red: 220/255, green: 20/255, blue: 60/255, alpha: 1)

labelMot.text = "Motivo da demissão"

labelMot.textAlignment = NSTextAlignment.Left

let textBtnMot = UIBarButtonItem(customView: labelMot)

toolBarMot.setItems([textBtnMot,flexSpaceMot,okBarBtnMot], animated: false)

motivoTextField.inputAccessoryView = toolBarMot

let inicio = inicioTextField.text
let fim = fimTextField.text



let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
let inicioDate: NSDate = dateFormatter.dateFromString(inicio)
let fimDate: NSDate = dateFormatter.dateFromString(fim)

let cal = NSCalendar.currentCalendar()
let unit = NSCalendarUnit.Day
let components = cal.components(unit, fromDate: inicioDate, toDate: fimDate, options: [])



}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


//Ação dos botões da toolbar Início

func donePressed(sender: UIBarButtonItem) {

inicioTextField.resignFirstResponder()

}

func tappedToolBarBtn(sender: UIBarButtonItem) {

let dateformatter = NSDateFormatter()

dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle

inicioTextField.text = dateformatter.stringFromDate(NSDate())

inicioTextField.resignFirstResponder()
}

//Ação dos botões da toolbar Fim

func donePressedFim(sender: UIBarButtonItem) {

fimTextField.resignFirstResponder()

}

func tappedToolBarBtnFim(sender: UIBarButtonItem) {

let dateformatter = NSDateFormatter()

dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle

fimTextField.text = dateformatter.stringFromDate(NSDate())

fimTextField.resignFirstResponder()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}

//Ação dos botões da toolbar Motivo

func donePressedMot(sender: UIBarButtonItem) {

motivoTextField.resignFirstResponder()

}

//Associação de valor para TexField Início

@IBAction func textFieldEditing(sender: UITextField) {

let datePickerViewInicio: UIDatePicker = UIDatePicker()

datePickerViewInicio.datePickerMode = UIDatePickerMode.Date

sender.inputView = datePickerViewInicio

datePickerViewInicio.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

}

func datePickerValueChanged(sender: UIDatePicker) {

let dateFormatter = NSDateFormatter()

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle

inicioTextField.text = dateFormatter.stringFromDate(sender.date)


}

//Associação de valor para TexField Início

@IBAction func textFieldEditingFim(sender: UITextField) {

let datePickerViewFim: UIDatePicker = UIDatePicker()

datePickerViewFim.datePickerMode = UIDatePickerMode.Date

sender.inputView = datePickerViewFim

datePickerViewFim.addTarget(self, action: Selector("datePickerValueChangedFim:"), forControlEvents: UIControlEvents.ValueChanged)

}

func datePickerValueChangedFim(sender: UIDatePicker) {

let dateFormatterFim = NSDateFormatter()

dateFormatterFim.dateStyle = NSDateFormatterStyle.MediumStyle

dateFormatterFim.timeStyle = NSDateFormatterStyle.NoStyle

fimTextField.text = dateFormatterFim.stringFromDate(sender.date)

}

//Associação de valor para TexField Início

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return motivos.count
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
motivoTextField.text = motivos[row]
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return motivos[row]
}

Error message

我对此很陌生,所以有人可以帮忙吗?

谢谢

最佳答案

你可以这样做:

func daysBetween(startDate: NSDate, endDate: NSDate) -> Int {
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(.Day, fromDate: calendar.startOfDayForDate(startDate), toDate: calendar.startOfDayForDate(endDate), options: [])
return dateComponents.day
}

关于ios - 填充两个 UITextField 的两个 UiDatePicker 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637809/

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