如何让用户选择日历事件的时间和日期-swift?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (106)

因此,我试图添加一个功能到我的应用程序,你可以添加一个日历事件。但是,我在时间和日期设置上有问题。我有一个文本开始日期和开始时间和结束日期。我遇到的问题是,当我在startDate和endDatetextField中输入日期时,输入的日期将是事件的开始日期和结束日期。另外,我也遇到了无法使startTime和EndTime正常工作的问题,因此输入的时间将是事件的开始和结束时间。代码如下。

@IBOutlet weak var eventNameTextField: UITextField!
@IBOutlet weak var descriptionTextField: UITextField!
@IBOutlet weak var startDateTextField: UITextField!
@IBOutlet weak var endDateTextField: UITextField!

@IBAction func doneButton(_ sender: Any) {
    addEventToCalendar(title: eventNameTextField.text!, description: descriptionTextField.text!, startDate: startDateTextField.text!, endDate: endDateTextField.text!)
}


let datePicker1 = UIDatePicker()
let datePicker2 = UIDatePicker()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    startDateTextField.delegate = self
    endDateTextField.delegate = self

    createDatePicker1()
    createDatePicker2()

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CalculatorViewController.textFieldShouldReturn))
    view.addGestureRecognizer(tap)

}

func createDatePicker1(){

    //assign datepicker to our textfield
    startDateTextField.inputView = datePicker1
    datePicker1.datePickerMode = .dateAndTime

    //create a toolbar
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    //add a done button on this toolbar
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneClicked1))

    toolbar.setItems([doneButton], animated: true)

    startDateTextField.inputAccessoryView = toolbar
    endDateTextField.inputAccessoryView = toolbar
}

func createDatePicker2(){

    //assign datepicker to our textfield
    endDateTextField.inputView = datePicker2
    datePicker2.datePickerMode = .dateAndTime

    //create a toolbar
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    //add a done button on this toolbar
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneClicked2))

    toolbar.setItems([doneButton], animated: true)

    endDateTextField.inputAccessoryView = toolbar
}

@objc func doneClicked1(){

    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
    dateFormatter.timeStyle = .short

    startDateTextField.text = dateFormatter.string(from: datePicker1.date)
    self.view.endEditing(true)

}

@objc func doneClicked2(){

    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
    dateFormatter.timeStyle = .short

    endDateTextField.text = dateFormatter.string(from: datePicker2.date)
    self.view.endEditing(true)

}

func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
    let eventStore = EKEventStore()

    eventStore.requestAccess(to: .event, completion: { (granted, error) in
        if (granted) && (error == nil) {
            let event = EKEvent(eventStore: eventStore)
            event.title = title
            event.startDate = startDate
            event.endDate = endDate
            event.notes = description
            event.calendar = eventStore.defaultCalendarForNewEvents
            do {
                try eventStore.save(event, span: .thisEvent)
            } catch let e as NSError {
                completion?(false, e)
                return
            }
            completion?(true, nil)
        } else {
            completion?(false, error as NSError?)
        }
    })
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/
提问于
用户回答回答于

看起来,你正在设计自己的界面,让用户输入事件的时间和日期。不要这样做。这是EKEventEditViewController的用途。它直接从Calendar应用程序向你提供事件编辑界面。可以工作。

扫码关注云+社区

领取腾讯云代金券