SwiftUI Create Picker 可以添加元素
SwiftUI Create Picker with possibility to add elements
我正在尝试构建一个带有类别的 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()
}
}
}
}
}
}
}
如评论所述,不确定您要达到的目标。
但据我了解,Menu
可能是一种更简单的方法:
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
}
}
我正在尝试构建一个带有类别的 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()
}
}
}
}
}
}
}
如评论所述,不确定您要达到的目标。
但据我了解,Menu
可能是一种更简单的方法:
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
}
}