选择器选择不更新

Picker selection not updating

我正在尝试 select 从可用帐户列表中选择一个默认帐号。数据来自FMDB数据selection。我创建了一个测试视图,其中包含两种类型的选择器。一个将我检索到的帐户列在一个数据记录数组中,该数组是一个 swift 结构。另一个选择器来自 select 颜色的在线示例。颜色“selection”绑定值按预期更新,但当两个帐户之一出现并 selected 时,我设置的“selection”绑定值不会更改。下面是我的测试视图中编译和运行的代码。当我 select 帐户值为 [12345678] 或 [12345679] 在选择器中显示为两行时,selection 绑定值不会改变。但是对于颜色 selection 值,它会更新。我在这里很困惑...

帐户记录的结构是:

    // Account record for FMDB
    struct AccountRecord: Hashable {
        var account_id: Int!
        var account_code: Int!
        var account_name: String!
        var running_balance: Double!
        var hidden_balance: Double!
        var actual_balance: Double!
    }
  import SwiftUI

  struct PickerTestView: View {
    
    @State private var selectedAccount = 0
    @State private var selectedColor = 0
    
    var acctRecords: [Accounts.AccountRecord] {
        return Accounts.shared.selectAllAccounts()
    }
    var colors = ["Red", "Green", "Blue", "Tartan"]
    
    var body: some View {
        VStack{
            Picker(selection: $selectedAccount, label: Text(""))
            {
                ForEach (self.acctRecords, id: \.self) { acct in
                    Text("\(acct.account_code!)")
                }
            }
            
            Text("selectedAccount = \(selectedAccount)")
                .font(.largeTitle)
            
            Picker(selection: $selectedColor, label: Text("Please choose a color")) {
                ForEach(0 ..< colors.count) {
                    Text(self.colors[[=11=]])
                }
            }
            
            Text("Selectedcolor = \(selectedColor)")
            Text("You selected \(colors[selectedColor])")
            
            
        }
    }
  }

  struct PickerTestView_Previews: PreviewProvider {
      static var previews: some View {
          PickerTestView()
      }
  }

尝试添加.onChange:

Picker(selection: $selectedAccount, label: Text("")) {
    ForEach (self.acctRecords, id: \.self) { acct in
        Text("\(acct.account_code!)").tag(acct.account_code) // <- add tag here
    }
}
.onChange(of: selectedAccount) {
    selectedAccount = [=10=]
}

有两件事正在发生:

  1. 您的 Picker 元素需要一个 .tag() 来告诉系统哪个元素属于哪个项目:
Picker(selection: $selectedAccount, label: Text(""))
            {
                ForEach (self.acctRecords, id: \.self) { acct in
                    Text("\(acct.account_code!)").tag(acct.account_id)
                }
            }
  1. SwiftUI 需要 selection 参数的类型和 tag 类型相同。因为在您的模型中,account_id 被定义为 Int! 而不是 Int,您的 selectedAccount 也需要是 Int!
@State private var selectedAccount : Int! = 0 

以下适用于嵌入的一些测试数据:
struct PickerTestView: View {
    @State private var selectedAccount : Int! = 1
    @State private var selectedColor = 0
    
    var acctRecords: [AccountRecord] {
        return [.init(account_id: 1, account_code: 1, account_name: "1", running_balance: 0, hidden_balance: 0, actual_balance: 0),
                .init(account_id: 2, account_code: 2, account_name: "2", running_balance: 0, hidden_balance: 0, actual_balance: 0),
                .init(account_id: 3, account_code: 3, account_name: "3", running_balance: 0, hidden_balance: 0, actual_balance: 0)
        ]
    }
    var colors = ["Red", "Green", "Blue", "Tartan"]
    
    var body: some View {
        VStack{
            Picker(selection: $selectedAccount, label: Text(""))
            {
                ForEach (self.acctRecords, id: \.self) { acct in
                    Text("\(acct.account_code!)").tag(acct.account_id)
                }
            }
            Text("selectedAccount = \(selectedAccount)")
                .font(.largeTitle)
        }
    }
}