【问题标题】:Disable button from loop when clicked单击时从循环中禁用按钮
【发布时间】:2020-12-25 06:30:08
【问题描述】:

目前我有一个 foreach 循环引入一个项目列表,每个项目都有一个执行功能的按钮。单击时如何禁用单个按钮,仅针对单个用户而不使用用户帐户?提前非常感谢!

额外背景: 每个项目都有一个本质上是“添加”按钮,它将 +1 添加到数据库中的一个字段,每个用户应该只被允许按一次按钮,因此我需要在单击后禁用它。截至目前,他们可以根据需要多次点击它,数据库中的数字会不断增加。

代码

ForEach(items.indices, id: \.self) { i in
HStack {
   Text(items[i].name)
   Spacer()
   Button(action: {
      self.performFunction()
      }) {
          Text("Yes")
             .font(.system(size: 14))
             .foregroundColor(Color.white)
             .padding(.horizontal)
       }
   }
}

【问题讨论】:

    标签: ios arrays swift button swiftui


    【解决方案1】:

    假设“Item”是一个结构或一个类,您总是可以添加一个布尔标志来指示该项目是否被选中。

    所以,例如,这样的事情:

    struct Item {
        var name: String
        var selected: Bool = false
    }
    
    struct ContentView: View {
        @State private var items: [Item] = [
            Item(name: "Bob"),
            Item(name: "Joe"),
            Item(name: "John Cena")
        ]
            
        var body: some View {
            ForEach(items.indices, id: \.self) { i in
                HStack {
                    Text(items[i].name)
                    Spacer()
                    Button(action: {
                        items[i].selected.toggle()
                    }) {
                        Text(items[i].selected ? "Yes" : "No")
                            .font(.system(size: 14))
                            .foregroundColor(.black)
                            .padding(.horizontal)
                    }
                }
            }
        }
    }
    
    

    【讨论】:

    • 这仅适用于代码,但我也在该按钮上调用了一个函数,该按钮似乎将其重置为 false,我不确定为什么 func yesTrigger() { let db = Firestore.firestore() db.collection("mainItems").document(itemID).collection("items").document("\(self.selection)").updateData([AnyHashable("yes"): FieldValue.increment(Int64(1))]) }
    【解决方案2】:

    目前尚不清楚上下文...但可能的解决方案是创建与items 相同长度的数组并使用它来存储单击状态,例如

    @State private var clicked: [Bool]  // initiate with array of false when `items` ready
    
    ...
    
       Button(action: {
          self.performFunction()
          self.clicked[i] = true      // << here !!
          }) {
              Text("Yes")
                 .font(.system(size: 14))
                 .foregroundColor(Color.white)
                 .padding(.horizontal)
           }
           .disabled(self.clicked[I])        // // << here !!
    
    

    【讨论】:

    • 这是有道理的,但是当我把这段代码放进去时,在预览部分我得到'View' initializer is inaccessible due to 'private' protection levelMissing argument for parameter 'clicked' in callInsert ', clicked:&lt;#[Bool]#&gt;'
    • 还为问题添加了额外的上下文
    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多