GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Challenge 1

Forums > 100 Days of SwiftUI


       Section("Enter value:)"){
         TextField("Input value", value: $value1, format: .number)
       }
       .textFieldStyle(.roundedBorder)
       .keyboardType(.decimalPad)

Given this fragment of code, whenever the TextField content is changed, I want to execute a function. I looked at " .onEditingChanged", but it seems to have been deprecated. Is there a way to do this?

2      

I would say all depends on what you need to achieve. Below first textfiled is executing function as soon as you change the value1, the second texfield is executing function as soon as you switching focus to that textfield.

struct ContentView: View {
    @State private var value1 = 0
    @State private var value2 = 0
    @FocusState private var isEditing

    var body: some View {
        Section {
            TextField("Enter value", value: $value1, format: .number)
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
                .onChange(of: value1) { oldValue, newValue in
                    print(oldValue)
                    print(newValue)
                }

            TextField("Enter value", value: $value2, format: .number)
                .foregroundStyle(isEditing ? .green : .red)
                .focused($isEditing).keyboardType(.decimalPad)
                .textFieldStyle(.roundedBorder)
                .keyboardType(.decimalPad)
        }
        .padding()
    }
}

2      

Thank you!

2      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.