Sunday, October 15, 2023
HomeiOS Developmentios - SwiftUI TextField Show Not Updating When Utilizing Integer Worth

ios – SwiftUI TextField Show Not Updating When Utilizing Integer Worth


I am attempting to make a view just like the Edit View in one of many Apple SwiftUI tutorials. Particularly, I wish to make one thing just like how they add attendees, however wish to do it with integers as a substitute of strings. I can principally get it to work, however for some cause the default worth would not wish to reset when clicking the + button to submit. Instance code is beneath:

import SwiftUI

struct Merchandise: Identifiable {
    public let id = UUID()
    var val: Int
}

struct BrokenView: View {
    @State personal var new_value = 0
    @State personal var l = [Item(val: 1), Item(val: 2), Item(val: 3)]
    
    var physique: some View {
        Kind {
            Part(header: Textual content("Objects")) {
                ForEach(l) { merchandise in
                    Textual content("(merchandise.val)")
                }
                HStack {
                    TextField("Worth", worth: $new_value, format: .quantity)

                    Button(motion: {
                        withAnimation {
                            l.append(Merchandise(val: new_value))
                            new_value = 0 // the worth will get set appropriately, however the brand new TextField retains the previous worth
                        }
                    }) {
                        Picture(systemName: "plus.circle.fill")
                    }
                    .disabled(new_value == 0)
                }
            }
        }
    }
}

I attempted to implement one thing like this reply right here, however I am nonetheless having the identical situation. What am I doing improper right here?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments