You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI/SwiftData自定义Picker选中状态无法更新问题

SwiftData模型下自定义Picker选中状态无法更新问题

因为常规Picker渲染图片过大,我实现了一个嵌套在Menu里的自定义Picker。但引入SwiftData的BonsaiModel数据后,Picker的选中状态无法通过代码正常更新。我尝试过解包、添加tag和id等方法,都没有效果。目前暂不关心图片更新问题,希望先解决Picker选中值变更的问题。

相关代码如下:

@Query private var bonsai: [BonsaiModel]
@State private var selectedBonsai: BonsaiModel?

Section("Select Bonsai:") {
    LabeledContent("Bonsai") {
        HStack {
            Image("d2")
                .renderingMode(Image.TemplateRenderingMode.original)
                .resizable()
                .scaledToFit()
                .frame(maxWidth: 60, maxHeight: 35)
                .clipShape(RoundedRectangle(cornerRadius: 3))
            Menu {
                // put the picker inside a menu, so users see the images in the menu
                Picker(selection: $selectedBonsai) {
                    ForEach(bonsai) { item in
                        Label {
                            if let spec = item.species {
                                Text(spec.name)
                                    .foregroundColor(Color("plainTextColor"))
                                    .font(.footnote)
                                    .fontWeight(.semibold)
                                    
                            } else {
                                Text("No species selected.")
                                    .foregroundColor(Color("plainTextColor"))
                                    .font(.footnote)
                                    .fontWeight(.semibold)
                                    
                            }
                        } icon: {
                            if let imageData = item.bonsaiImageData, // unwrapp data from item.image and store in imageData
                               let uiImage = UIImage(data: imageData) { // Put imgageData in UIImage and store image in uiImage
                                Image(uiImage: uiImage)
                            } else {
                                Image(systemName: "tree")
                            }
                        }.id(item)
                    }
                    
                    
                } label: { }
            } label: {
                HStack {
                    if let species = selectedBonsai {
                        Text(species.name)
                            .foregroundColor(Color("plainTextColor"))
                            .font(.footnote)
                            .fontWeight(.semibold)
                    } else {
                        Text("No species selected.")
                            .foregroundColor(Color("plainTextColor"))
                            .font(.footnote)
                            .fontWeight(.semibold)
                    }
                    Image(systemName: "chevron.up.chevron.down")
                }
                .tint(.secondary)
                .imageScale(.small)
            }
        }
    }
}

Picker界面截图:
Picker界面截图

内容的提问来源于stack exchange,提问作者DennisHH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:08:31