TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Challenge 1

Forums > 100 Days of SwiftUI

       Section("Enter value:)"){
         TextField("Input value", value: $value1, format: .number)

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?


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)
                .onChange(of: value1) { oldValue, newValue in

            TextField("Enter value", value: $value2, format: .number)
                .foregroundStyle(isEditing ? .green : .red)


Thank you!


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.