Sunday, December 31, 2023
HomeiOS DevelopmentHow can I faucet off of a @FocusedField in SwiftUI?

How can I faucet off of a @FocusedField in SwiftUI?


I’m new to SwiftUI and beginning out by making an attempt to create a easy view that permits you to add gadgets to a NavigationStack utilizing SwiftData. This is my code to date:

import SwiftData
import SwiftUI

struct ItemList: View {
    @Atmosphere(.modelContext) non-public var modelContext
    @Question non-public var gadgets: [Item]
    @State non-public var isAdding = false
    @State non-public var newItemName = ""
    @FocusState non-public var focusedField: String?
    
    var physique: some View {
        NavigationStack {
            Checklist {
                Part(header: Textual content("Gadgets")) {
                    ForEach(gadgets) { merchandise in
                        NavigationLink {
                            Textual content("Placeholder")
                        } label: {
                            Textual content(merchandise.title)
                        }
                        .swipeActions(edge: .trailing) {
                            Button(position: .harmful) {
                                modelContext.delete(merchandise)
                            } label: {
                                Label("Delete Merchandise", systemImage: "trash.fill")
                            }
                        }
                    }
                    if isAdding {
                        TextField("Title", textual content: $newItemTitle, onCommit: {
                            if !newItemTitle.isEmpty {
                                let newItem = Merchandise(title: newItemTitle)
                                modelContext.insert(newItem)
                            }
                            isAdding = false
                            newItemTitle = ""
                            focusedField = nil
                        })
                        .targeted($focusedField, equals: "newItem")
                        .onAppear {
                            focusedField = "newItem"
                        }
                        .onDisappear {
                            isAdding = false
                            newItemTitle = ""
                            focusedField = nil
                            appearedCount = 0
                        }
                    }
                }
            }
            .listStyle(.insetGrouped)
            .toolbar {
                ToolbarItem {
                    Button {
                        isAdding = true
                    } label: {
                        Label("New Merchandise", systemImage: "plus")
                    }
                }
            }
        }
    }
}

#Preview {
    ItemList()
        .modelContainer(for: Merchandise.self, inMemory: true)
}

In order you’ll be able to see, it creates an inventory of any gadgets saved in SwiftData proven as NavigationLinks, and there is additionally a toolbar button that permits you to add a brand new merchandise. While you click on that button, it reveals a hidden textual content subject on the backside of the listing and focuses it, mentioning the keyboard. That half is nice and dealing as supposed.

Due to the onCommit handler, if the consumer presses Return on the keyboard, it’s going to both dismiss the textual content subject (in the event that they have not crammed something out), or create a brand new merchandise earlier than dismissing the sphere (if they’ve). However what’s lacking is that if the consumer faucets wherever exterior of the textual content subject, it nonetheless stays energetic. What I want to have occur is that if they faucet wherever else within the view, exterior of that textual content subject, and in the event that they have not typed something but, it ought to merely dismiss the textual content subject.

I attempted including a TapGesture to the view to do that, however it did not work, so now I am caught. How can I accomplish this?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments