12.6 C
New York
Tuesday, April 1, 2025

ios – SwiftUI: Find out how to get worth from TextField offered in a backside sheet base on a price associated to the chosen merchandise on the checklist?


That is the way it look within the app:

enter image description here
enter image description here

On the left there’s a easy checklist created from ForEach. When consumer faucet any ingredient on that checklist BottomSheet is offered with a Binded worth for chosen merchandise. How can I get that worth and replace NSManagedObject in core information when consumer faucet save button? Is there a easy and simple method to do that?

The identical state of affairs in code:

struct SettingsView: View {
    @State var isTextInputPresented = false
    @State var editingTextFieldValue = ""
    non-public var onSave: ((String) -> Void)? = nil

    var physique: some View {
        ZStack {
            ScrollView {
              ForEach(customers, id: .self) { consumer in //customers are from @FetchRequest
                HStack {
                    TextLabel(consumer.identify, coloration: theme.secondTeamColor)
                        .padding(EdgeInsets(vertical: 10))
                    Spacer()
                }
                .onTapGesture {
                    editingTextFieldValue = consumer.identify
                    isTextInputPresented = true
                    // can not assign onSave handler right here🤷🏼‍♂️
                }
              }
            }
            BottomSheetView(title: "Edit enter", isShowing: $isTextInputPresented) {
                TextInputView(textual content: $editingTextFieldValue, onSave: onSave)
            }
        }
    }
}


import SwiftUI

struct TextInputView: View {
    @Atmosphere(Theme.self) non-public var theme
    @Binding var textual content: String
    @FocusState non-public var focus: Bool
    var onSave: ((String) -> Void)?
    var physique: some View {
        HStack(spacing: 20) {
            TextField("", textual content: $textual content, immediate: Textual content("Placeholder").foregroundColor(.grey))
                .padding(10)
                .multilineTextAlignment(.middle)
                .font(.system(measurement: 24))
                .foregroundStyle(theme.backgroundColor)
                .tint(theme.backgroundColor.opacity(0.4))
                .centered($focus)
                .cornerRadius(10)
                .overlay(
                    RoundedRectangle(cornerRadius: 10)
                        .stroke(theme.backgroundColor.opacity(0.5), lineWidth: 3)
                )
            Button {
                onSave?(textual content)
            } label: {
                Picture(systemName: "checkmark")
            }
            .font(.daring(withSize: 22))
            .body(width: 56, top: 56)
            .background(theme.backgroundColor)
            .foregroundStyle(theme.textColor)
            .cornerRadius(10)
        }
        .padding(20)
        .onAppear {
            focus = true
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles