【问题标题】:SwiftUI Allowing a button to only be tapped onceSwiftUI 允许一个按钮只被点击一次
【发布时间】:2020-08-24 21:13:55
【问题描述】:

我有一个将 firebase 值加一的按钮,但是我试图只允许点击该按钮一次。尝试了一些方法,但无法正常工作,有没有简单的方法可以让它正常工作?

Button(action: {
    let db = Firestore.firestore()

    let like = Int.init(self.likes)!
    db.collection("posts").document(self.id).updateData(["likes": "\(like + 1)"]) { err in
        if err != nil {
            print(err)
            return
        }

        print("updated...")
    }
}) {
    Image(systemName: "arrow.up.square")
        .frame(width: 26, height: 26)
}

【问题讨论】:

    标签: button swiftui


    【解决方案1】:

    这是一个简单的演示:

    struct ContentView: View {
        @State var buttonTapped = false
    
        var body: some View {
            Button(action: {
                self.buttonTapped.toggle()
                // other actions...
            }) {
                Text("Tap me")
            }
            .disabled(buttonTapped)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多