1

I have SwiftUI textfield. I submit same values and process these values in an other class. According to process in this class I have to replace values in textField with previous data programmatically.

As usual this a pain in swiftUI. How can I do it ?

2
  • You'd rather need a temporary variable in other class, but better provide minimal reproducible example to demo your problem. Commented Apr 23, 2022 at 7:12
  • Is there a need for demo I want to change textField values programmatically after textField shown on screen in short. Commented Apr 23, 2022 at 7:14

1 Answer 1

1

Why it have to be pain. In UIView textField.text = "" was enough. I am sure they have their reasons above my grasp.

But while using SwiftUI I did it like that first you put your variables in

class Shared: ObservableObject {
static let shared = Shared()

@Published var texEditsDefaults = Edits().defaultValues

}

then in process class

 class Calculation {

 @ObservedObject var calculation = Shared.shared


   // do something

   texEditsDefaults = newValues

 }

and

struct TextValuesView: View {
  @ObservedObject var calculation = Shared.shared
   var body: some View {
    ForEach(0..<calcNames.count, id: \.self) { calcIndex in               
      TextField(calcNames[calcIndex], text: $calculation.texEditsDefaults[calcIndex])
         .onSubmit{                                     
           // send values class calculation
         }
       }      
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.