Swiftui创建有可能添加元素的选择器

发布于 2025-01-29 04:52:46 字数 2212 浏览 3 评论 0原文

我正在尝试构建一个具有类别的Swift应用程序,我为我的类别创建了一个选择器,但是我希望能够在选择器本身内创建一个类别。但是我不知道该如何正确地做,我尝试了一些粗略的事情,但是它有点丑陋,而且正如我想做的那样工作。

这是我的工作选择器

import SwiftUI

struct CategoryPicker: View {
    @Binding var selection: TransactionCategory?
    @Binding var userData: UserData
    
    var body: some View {
        Picker("category", selection: $selection) {
            CategoryView(category: nil)
                .tag(nil as TransactionCategory?)
            ForEach(userData.transactionsCategories) { category in
                CategoryView(category: category)
                    .tag(category as TransactionCategory?)
            }
        }
    }
}

,这是我到目前为止尝试的方法,但它使得非常糟糕

struct CustomPickerExperiments: View {
    @Binding var selection: TransactionCategory?
    @Binding var userData: UserData
    @Environment(\.presentationMode) var presentation
    
    @State var color = Color.white
    @State var name = ""
    
    var body: some View {
        List {
            Section(header: Text("new-category")) {
                NavigationLink("add-category", destination: CategoryEditView(userData: $userData))
            }
            Section(header: Text("categories")) {
                CategoryView(category: nil)
                    .tag(nil as TransactionCategory?)
                    .onTapGesture {
                        if(selection != nil) {
                            selection = nil as TransactionCategory?
                            self.presentation.wrappedValue.dismiss()
                        }
                    }
                ForEach(userData.transactionsCategories) { category in
                    CategoryView(category: category)
                        .tag(category as TransactionCategory?)
                        .onTapGesture {
                            if(selection != category) {
                                selection = category as TransactionCategory?
                                self.presentation.wrappedValue.dismiss()
                            }
                        }
                }
            }
        }
    }
}

I'm trying to build a swift app with categories, I created a Picker for my categories but I would like to be able to create a Category inside the Picker itself. But I can't figure how to do it properly, I've tried something sketchy but it's kinda ugly and working as I would like to do.

This is my working picker

import SwiftUI

struct CategoryPicker: View {
    @Binding var selection: TransactionCategory?
    @Binding var userData: UserData
    
    var body: some View {
        Picker("category", selection: $selection) {
            CategoryView(category: nil)
                .tag(nil as TransactionCategory?)
            ForEach(userData.transactionsCategories) { category in
                CategoryView(category: category)
                    .tag(category as TransactionCategory?)
            }
        }
    }
}

And this is what I tried so far but it's rendering pretty bad

struct CustomPickerExperiments: View {
    @Binding var selection: TransactionCategory?
    @Binding var userData: UserData
    @Environment(\.presentationMode) var presentation
    
    @State var color = Color.white
    @State var name = ""
    
    var body: some View {
        List {
            Section(header: Text("new-category")) {
                NavigationLink("add-category", destination: CategoryEditView(userData: $userData))
            }
            Section(header: Text("categories")) {
                CategoryView(category: nil)
                    .tag(nil as TransactionCategory?)
                    .onTapGesture {
                        if(selection != nil) {
                            selection = nil as TransactionCategory?
                            self.presentation.wrappedValue.dismiss()
                        }
                    }
                ForEach(userData.transactionsCategories) { category in
                    CategoryView(category: category)
                        .tag(category as TransactionCategory?)
                        .onTapGesture {
                            if(selection != category) {
                                selection = category as TransactionCategory?
                                self.presentation.wrappedValue.dismiss()
                            }
                        }
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2025-02-05 04:52:46

如评论不确定您要实现的目标。
但是,据我了解,菜单可能是一种更简单的方法:

struct ContentView: View {
    
    @State private var categories = [
        Category(name: "Beer"),
        Category(name: "Red Wine"),
        Category(name: "Water"),
        Category(name: "Juice")
    ]
    
    @State private var selection: Category?
    @State private var showingSheet = false
    @State private var newName = "New Category"

    
    var body: some View {
        VStack {
            Menu(selection?.name ?? "no selection") {
                Button("New Category ...") {
                        showingSheet = true
                    }

                ForEach(categories) { category in
                    Button(category.name) { selection = category }
                }
            }
        }
        
        .sheet(isPresented: $showingSheet, onDismiss: {
            if !newName.isEmpty {
                let newCat = Category(name: newName)
                categories.append(newCat)
                selection = newCat
                newName = ""
            }
        }, content: {
            VStack(alignment: .leading) {
                Text("Add Category").font(.headline)
                TextField("New Name", text: $newName)
            }.padding()
        })
    }
    
    
    struct Category: Identifiable, Hashable {
        let id = UUID()
        var name: String
    }

}

As commented not sure what you try to achieve.
But as I understand it a Menu might be an easier way to go:

struct ContentView: View {
    
    @State private var categories = [
        Category(name: "Beer"),
        Category(name: "Red Wine"),
        Category(name: "Water"),
        Category(name: "Juice")
    ]
    
    @State private var selection: Category?
    @State private var showingSheet = false
    @State private var newName = "New Category"

    
    var body: some View {
        VStack {
            Menu(selection?.name ?? "no selection") {
                Button("New Category ...") {
                        showingSheet = true
                    }

                ForEach(categories) { category in
                    Button(category.name) { selection = category }
                }
            }
        }
        
        .sheet(isPresented: $showingSheet, onDismiss: {
            if !newName.isEmpty {
                let newCat = Category(name: newName)
                categories.append(newCat)
                selection = newCat
                newName = ""
            }
        }, content: {
            VStack(alignment: .leading) {
                Text("Add Category").font(.headline)
                TextField("New Name", text: $newName)
            }.padding()
        })
    }
    
    
    struct Category: Identifiable, Hashable {
        let id = UUID()
        var name: String
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文