将Swiftui选择器与课堂
我一直在努力让选择器从数组中返回对象。我的印象是,我要做的就是将对象作为标签包括在内,而选择最终将作为该对象。显然不是。任何帮助将不胜感激。
我的实际应用使用从Coredata提取的信息。
该测试应用程序中的预期结果是将选择的选择显示了做出选择的项目的右侧。
import SwiftUI
class Choice: Identifiable, Hashable
{
let id: UUID
var name = ""
init()
{
self.id = UUID()
}
init(_ name: String)
{
self.id = UUID()
self.name = name
}
static func == (lhs: Choice, rhs: Choice) -> Bool
{
return lhs.name < rhs.name
}
func hash(into hasher: inout Hasher)
{
hasher.combine(name)
}
}
class Stuff: Identifiable
{
let id: UUID
var title: String
var choice: Choice?
init(_ title: String)
{
self.id = UUID()
self.title = title
}
}
struct ContentView: View
{
let items = [Stuff("ABC"), Stuff("XYZ")]
let choices = [Choice("ONE"),Choice("Two"), Choice("Three)")]
@State var choice: Choice?
var body: some View
{
VStack
{
ForEach(items)
{ item in
HStack
{
Text(item.title)
Picker("Choice", selection: $choice)
{
ForEach(choices)
{
c in
Text(c.name).tag(c)
}
}
}
}
Spacer()
ForEach(items)
{
item in
HStack
{
Text(item.title)
Spacer()
if let c = item.choice
{
Text(c.name)
} else
{
Text("No choice")
}
}
}
}
}
}
I've been struggling to get Picker to return an object from an array. I was of the impression that all I needed to do was include the object as a tag and the selection would end up as that object. Apparently not. Any help would be greatly appreciated.
My actual app uses information extracted from CoreData.
The expected result in this test app is to show the selection to the right of the item where the choice was made.
import SwiftUI
class Choice: Identifiable, Hashable
{
let id: UUID
var name = ""
init()
{
self.id = UUID()
}
init(_ name: String)
{
self.id = UUID()
self.name = name
}
static func == (lhs: Choice, rhs: Choice) -> Bool
{
return lhs.name < rhs.name
}
func hash(into hasher: inout Hasher)
{
hasher.combine(name)
}
}
class Stuff: Identifiable
{
let id: UUID
var title: String
var choice: Choice?
init(_ title: String)
{
self.id = UUID()
self.title = title
}
}
struct ContentView: View
{
let items = [Stuff("ABC"), Stuff("XYZ")]
let choices = [Choice("ONE"),Choice("Two"), Choice("Three)")]
@State var choice: Choice?
var body: some View
{
VStack
{
ForEach(items)
{ item in
HStack
{
Text(item.title)
Picker("Choice", selection: $choice)
{
ForEach(choices)
{
c in
Text(c.name).tag(c)
}
}
}
}
Spacer()
ForEach(items)
{
item in
HStack
{
Text(item.title)
Spacer()
if let c = item.choice
{
Text(c.name)
} else
{
Text("No choice")
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择和标签应按类型匹配,目前不匹配,因此
时还需要修复修复
在用Xcode 13.4 / ios 15.5进行测试
Selection and tag should match by type, currently they are not, so fix is
also correction needed in
Tested with Xcode 13.4 / iOS 15.5
下面的建议肯定有所帮助,但正如指出的那样,有几个错误/假设使原始方法粗略。我通过创建一个单独的视图来展示每行/(列表项目)并将选择器分开,从而结合了建议并更改了策略。
The below suggestions definitely helped but as pointed out, there were several errors/assumptions that made the original approach sketchy. I have incorporated the suggestions and changed strategy by creating a separate view to present each line/(list item) and to separate the pickers.