Swiftui创建有可能添加元素的选择器
我正在尝试构建一个具有类别的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论不确定您要实现的目标。
但是,据我了解,
菜单
可能是一种更简单的方法:As commented not sure what you try to achieve.
But as I understand it a
Menu
might be an easier way to go: