计算与TextFields

时间:2015-05-05 01:59:35

标签: swift date nsdate textfield

我试图计算两个文本字段中使用日期选择器选取的两个日期之间的差异,并在UILabel中显示结果。

具有弹出日期选择器和日期格式的第一部分工作正常,但是当我尝试计算日期(代码已经过测试并在Play Ground中工作)时,Xcode会产生大量错误。我认为这表明我对swift的理解很少,而且我很确定这些错误是因为一些根本性的错误,我无法捕捉到。

任何建议都将受到高度赞赏。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var startDate: UITextField!
    @IBOutlet weak var endDate: UITextField!
    @IBOutlet weak var workingDays: UILabel!


    // Start Date Text Field- action and format for DatePicker
    @IBAction func startDateTextFieldEditing(sender: UITextField) {
        var datePickerView:UIDatePicker = UIDatePicker()
        datePickerView.datePickerMode = UIDatePickerMode.Date
        sender.inputView = datePickerView
        datePickerView.addTarget(self, action: Selector("startDatePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
    }

    func startDatePickerValueChanged(sender: UIDatePicker) {
        var dateformatter = NSDateFormatter()
        dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
        startDate.text = dateformatter.stringFromDate(sender.date)
        println("Start Date is " + startDate.text)
    }

    // End Date Text Field - action and format for DatePicker
    @IBAction func endDateTextFieldEditing(sender: UITextField) {
        var datePickerView:UIDatePicker = UIDatePicker()
        datePickerView.datePickerMode = UIDatePickerMode.Date
        sender.inputView = datePickerView
        datePickerView.addTarget(self, action: Selector("endDatePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
    }

    func endDatePickerValueChanged(sender: UIDatePicker) {
        var dateformatter = NSDateFormatter()
        dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
        endDate.text = dateformatter.stringFromDate(sender.date)
        println("End Date is " + endDate.text)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(true)
    }

    // Convert the String to NSDate
    var start = startDate.Text
    var end = endDate.text
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    let startDate: NSDate = dateFormatter.dateFromString(start)!
    let endDate: NSDate = dateFormatter.dateFromString(end)!

    // Diference between two NSDate -> Int +1
    let cal = NSCalendar.currentCalendar()
    let unit: NSCalendarUnit = .DayCalendarUnit
    let components = cal.components(unit, fromDate: startDate, toDate: endDate, options: nil )
    let calulatedDays = components.day + 1

    // Set String for UILabel workingDays
    workingDays.text = "\(calculatedDays)"
    println(workingDays)

}

2 个答案:

答案 0 :(得分:1)

我正在做的第一个错误是我想将TextField的值作为String并将其签名为变量。 由此生成的错误消息是

  

'ViewController.Type'没有名为'inputTaxtField'的成员

再一次,请原谅我上面的长代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var inputTextField: UITextField!

    var myString = inputTextField.text

}

答案 1 :(得分:0)

您在原始帖子中显示3个出口,但没有一个出现名称inputTaxtField。在您的其他帖子中,您会显示inputTextField。不同的拼写。这些事情中的任何一个都可能是您的问题的原因