Search code examples
iosswiftuitextfieldxcode11

How to get te text of a textfield from inside a function?


I want to make an app that has 2 textfields, 4 buttons and a Text view. 1 button that clears the content of the first textfield and another button that inputs the current time rounded to the nearest 4 and the Text view to display the total time in minutes. Same with the other textfield. It's an app to calculate the total time spent on something in minutes.

app layout

I also want to limit the amount of possible insertable characters to 5 as a time has a maximum of 5 characters as in "15:00". I have been able to make an observable object:

class TextFieldManager: ObservableObject {

    let characterLimit = 5

    @Published var userInput = "" {
        didSet {
            // Limit the max length of the field
            if userInput.count > characterLimit {
                userInput = String(userInput.prefix(characterLimit))
            } else if userInput.count >= 4 {
                // call the setTotalTime func or something to set totalTime
            }
        }
    }
}

which works really well.

I am also able to clear the textfield and set the current time rounded to the nearest 5, but i am unable to limit the characters to 5 AND have the buttons do something at the same time.

So right now it's either being able to: - Limit characters to a maximum of 5

OR being able to:

  • clear textfield with a button
  • input time now rounded to nearest 5 into textfield with a button.

If i go with the observable object method, when i have this above the body block of my ContentView:

@ObservedObject var startTimeManager = TextFieldManager()
@ObservedObject var endTimeManager = TextFieldManager()

and this inside the body for the textfields:

TextField("hh:mm", text: $startTimeManager.userInput)
TextField("hh:mm", text: $endTimeManager.userInput)

and the TextFieldManager class as shown above, than now i don't know how to get the value of the Textfield anymore.

If inside the input the current time button i try to set the time by doing

$startTimeManager.userInput = "whatever the current time is"

i get an error saying that i can't change the value of a binding<String> type something something. Likewise i also can't clear the textfield in the same way.

Also i would like to call a function inside this part:

} else if userInput.count >= characterLimit {
    // call the setTotalTime func or something to set totalTime
}

I have a Functions.swift file where both the TextFieldManager class is and my function that i want to call, but if i try to call a function inside here, it says that the function doesn't exist? And inside the function i again would like to have access to the textfield values at the time of the call, but i don't know how to read the value of the textfields from inside the function.

I hope i am making sense and that someone is able to help me, or point me in the right direction. I made the same app for android (android studio), windows (python3) and Mac (python3), but this iphone and Xcode thing really doesn't want to work. I have watched a bunch of tutorial videos and guides, but none are trying to do what i am trying to do. Like i said i can get either to work, but never together and in both cases i am unable to somehow access the textfield values inside the function. I feel like i should be so close, but something is not coming together for me in my head.

Also while i'm at it, in Python 3 to catch all errors and let them pass silently i can do:

Try:
    break_my_stuff = int("Break my stuff)"
    ignore_some_more_stuff = int("ignore some more stuff)"
    etc.
    etc.
except Exception:
    # catch silently and do nothing
    pass

Is there something similar in swift, because

do {
    breakMyStuff = whatever might make something break in swift
    ignoreSomeMoreStuff = whatever might make something break in swift
    etc.
    etc.
} catch {
    // do nothing and pass silently
}

doesn't work because it needs something to try and i wasn't able to try a complete section like i can with Python.


Solution

  • If inside the input the current time button i try to set the time by doing

    $startTimeManager.userInput = "whatever the current time is"

    you don't need binding in this case, just assign property in regular way

    self.startTimeManager.userInput = "whatever the current time is"