有没有一种方法可以让一个按钮在SwiftUI中运行多个功能? [英] Is there a way to have a button run multiple functions in SwiftUI?

查看:64
本文介绍了有没有一种方法可以让一个按钮在SwiftUI中运行多个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让一个按钮执行注销,并通过一个按钮将布尔值更改为false.有什么办法可以做到这一点,或者在SwiftUI中有类似的东西吗?

I'm trying to have a button perform a sign out and change a boolean to false with one button. Is there any way to do this is or something similar in 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))
                        }
                   }

推荐答案

您不必执行试图将多个参数传递给 action 的操作,而只需执行关闭操作,并根据需要执行许多操作即可.看起来更像这样:

Instead of trying to pass multiple arguments into action you can just execute the closure and have it do as many things as you want. That would look more like this:

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))
     }
}

这篇关于有没有一种方法可以让一个按钮在SwiftUI中运行多个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆