【问题标题】:Is there a way to have a button run multiple functions in SwiftUI?有没有办法让一个按钮在 SwiftUI 中运行多个功能?
【发布时间】:2020-04-04 01:07:25
【问题描述】:

我正在尝试让一个按钮执行注销并使用一个按钮将布尔值更改为 false。在 SwiftUI 中是否有任何方法可以做到这一点或类似的东西?

Button(action: GIDSignIn.sharedInstance()!.signOut, self.settings.settingsView.toggle()) {
           ZStack {
               Rectangle()
                    .frame(width: 300, height: 50)
                    .cornerRadius(5)
                    .foregroundColor(Color(.systemGray))
               Text("Sign Out")
                    .foregroundColor(.primary)
                     .font(Font.system(size: 25))
                        }
                   }

【问题讨论】:

    标签: swift button swiftui


    【解决方案1】:

    与其尝试将多个参数传递给action,您只需执行闭包并让它做任何您想做的事情。看起来更像这样:

    Button(action: {
        GIDSignIn.sharedInstance()!.signOut
        self.settings.settingsView.toggle()
    }) {
        ZStack {
            Rectangle()
                 .frame(width: 300, height: 50)
                 .cornerRadius(5)
                 .foregroundColor(Color(.systemGray))
            Text("Sign Out")
                 .foregroundColor(.primary)
                 .font(Font.system(size: 25))
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 2011-08-01
      • 2020-07-29
      • 2022-10-04
      • 2018-11-07
      • 2021-08-16
      • 2012-10-07
      • 1970-01-01
      相关资源
      最近更新 更多