I have been doing a research with no luck on how to perform this operation and I still don’t get quite right the NSdate and the way to perform operations.
I have an app where a timeText(UITextField) is pressed a UIDatPicker pops up and the user can select the time desired with the wheel. Then, once doneBTN(from the toolBar) is pressed it populates the time desired on that textfield.
What I am looking for is to display on Tminus3 (UILabel), Tminus10 (UILabel), Tminus20 (UILabel) respectively, the result of the timeText (selected from the user) minus 3 min (Tminus3), minus 10 min (Tminus10), minus 20min (Tminus20) respectively on each label.
I.e. The user press the timeText and select the time, lets say 11:30. When I press updateBTN I would like to get the value of Tminus3.text = 11:27, Tminus10.text = 11:20, Tminus20.text = 11:10.
This is the initial code.
Thanks in advance
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timeText: UITextField!
let timePicker = UIDatePicker()
@IBOutlet weak var Tminus3: UILabel!
@IBOutlet weak var Tminus10: UILabel!
@IBOutlet weak var Tminus20: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
createTimeP()
}
func creteToolB() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBTN = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneBTN], animated: true)
return toolbar
}
func createTimeP() {
timePicker.preferredDatePickerStyle = .wheels
timePicker.datePickerMode = .time
timePicker.locale = NSLocale(localeIdentifier: "UTC") as Locale
timePicker.minuteInterval = 5
timeText.inputView = timePicker
timeText.inputAccessoryView = creteToolB()
}
@objc func donePressed() {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
self.timeText.text = timeFormatter.string(from: timePicker.date)
self.view.endEditing(true)
}
@IBAction func **updateBTN**(_ sender: Any) {
// from the timeText.text = "HH:mm" get the value —> minus3.text(label) minutes, minus10.text()label minutes, minus20.text(label) minutes.
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…