填充两个UITextField

时间:2015-11-10 19:17:03

标签: ios swift uitextfield uidatepicker

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

我在论坛中得到了这个,但它没有用。

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

我对此很新,所以有人可以提供帮助吗?

由于

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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
}