Home > Enterprise >  how to Store value of UITextField
how to Store value of UITextField

Time:02-16

i'm using time pickers and i want to store the two values to use them in calculating price, store in firebase... etc. this is my UI to simplify the idea enter image description here

the problem is that i dont know which part i can use to store the value of start time andd end time (the numbers in UI are written in arabic sorry)

this is my code, According to my code. how can i store the textfield values in variable? i mean should i write ( let EndTime = EndTimeText.text ) ? attached my code

@IBOutlet weak var EndTimeTxt: UITextField!
func createTimePicker() {
            
            StartTimeTxt.textAlignment = .center
            EndTimeTxt.textAlignment = .center
            
            //tool bar
            let toolbar = UIToolbar()
            toolbar.sizeToFit()
            
            //bar button(
           let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
           toolbar.setItems([doneBtn], animated: true)
        
            //assign tool bar
            StartTimeTxt.inputAccessoryView = toolbar
            EndTimeTxt.inputAccessoryView = toolbar
            
            // assign time picker to the txt field
          StartTimeTxt.inputView = StartTimePicker
          EndTimeTxt.inputView = EndTimePicker
        
     
            //time picker mode
           /* StartTimePicker.datePickerMode = .time
            EndTimePicker.datePickerMode = .time*/
        let calendar = Calendar.current
        //First range
        let startTime = Date()
        let startRangeEnd = calendar.date(byAdding: DateComponents(minute: 30), to: startTime)!
        let startRange = startTime...startRangeEnd
        //Second range
        let endTime = calendar.date(byAdding: DateComponents(minute: 1), to: startRangeEnd)!
        let endRangeEnd = calendar.startOfDay(for: calendar.date(byAdding: DateComponents(day: 1), to: startTime)!)
        let endRange = endTime...endRangeEnd
        StartTimePicker.date = startTime
        StartTimePicker.minimumDate = startTime
        StartTimePicker.maximumDate = startRangeEnd
        EndTimePicker.date = endTime
        EndTimePicker.minimumDate = endTime
        EndTimePicker.maximumDate = endRangeEnd
             if #available(iOS 13.4, *)  {
                   StartTimePicker.preferredDatePickerStyle = .wheels
                   EndTimePicker.preferredDatePickerStyle = .wheels
               }
       
        } ```

CodePudding user response:

Firstly confirm your class to "UITextFieldDelegate" protocol and set text field delegate to self.

There are several Textfield Delegate methods which would help you to deal with Text fields. out of which use below delegate method to get text field text

func textFieldDidEndEditing(textField: UITextField) {

print("(textField.text)")

}

CodePudding user response:

So, from what I am understanding and what I think is that you should do is first create outlets of start text and end text. After that, you could do is like starttext.text. This basically returns whatever you typed in the text field or you could also do is that add a UITextFieldDelegate Protocol as it has some functions that can help you do this as well.

I don't know if this is the right answer but I hope I helped you.

  • Related