【问题标题】:How do I change one button's action when another is clicked (SwiftUI)?单击另一个按钮时如何更改一个按钮的操作(SwiftUI)?
【发布时间】:2021-10-26 14:41:05
【问题描述】:

在我的应用程序中,我有一个按钮,每次按下它都会输出一个特定的警报。但是,我想要它,以便当我单击该按钮 10 次时,按钮的操作和标签会发生变化,因为我想将其变成重新启动按钮。我该怎么做?

换句话说,当一个按钮没有名称时,我如何引用它并修改它的属性?

'''迅速

           Button(action: { alertIsVisible = true
                
            }) {
                Text(buttonText)
                    .fontWeight(.bold)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(20)
                    
                
            }
            .alert(isPresented: $alertIsVisible, content: {
                let roundedValue = Int(sliderValue.rounded())
                let points = game.points(sliderValue: roundedValue)
                
                    return Alert(title: Text("Hello!"), message: Text("The slider's value is \(roundedValue).\n" + "You scored \(points) points this round"), dismissButton: .cancel(
                        
                                    {game.target = Int.random(in: 1...100)
                                        roundTracker += 1
                                        scoreTracker += points
                                    }
                        ))
                    
                })

'''

【问题讨论】:

  • 你能显示你的代码吗?
  • 您不能引用该按钮,但您可以使用 @State 或 @Published 文本作为 label 并为操作添加条件。喜欢if count == 10重置else递增计数

标签: swift button swiftui reference


【解决方案1】:

这是一个示例,其中计数器存储为@State。当counter 到达10 时,它会显示“重新启动”按钮。

struct ContentView: View {
    @State private var counter = 0

    var body: some View {
        if counter < 10 {
            Button("Press") {
                print("button pressed: \(counter)")
                counter += 1
            }
        } else {
            Button("Restart") {
                print("restart...")
                counter = 0
            }
        }
    }
}

结果:

【讨论】:

  • 好的,但是如何推断您正在修改示例中的前一个按钮?每当我在计算 10 轮后尝试制作重新启动按钮时,它都会显示“'Button
  • @Krish 我的答案中的代码有效。你改变了什么?好像你可能没有返回 Button 视图,可能还有另一个错误?
  • 我的代码在上面。我尝试在“scoreTracker += points”下方添加一个 if 语句,但它给了我我告诉你的错误。我是 Swift 的初学者,所以你知道我该如何解决这个问题吗?我希望在单击警报的取消按钮后执行代码,这就是我把它放在那里的原因。
  • 我实际上能够解决它。非常感谢您的帮助!
  • @Krish 如果现在一切正常,那真是个好消息!接受答案将不胜感激,并帮助未来的用户解决同样的问题!
【解决方案2】:

您可以根据counter 变量的值改变Button 的label 和action(如果使用propertywrapper @State 很好地声明)

我在这里使用了三元运算符:

struct CounterView: View {
    @State private var counter: Int = 0
    var exceeded: Bool  { counter >= 10 }
    func restart() {
        // do something
        counter = 0
    }
    func increment() {
        counter += 1
    }
    
    var body: some View {
        Button(exceeded ? "Restart" : "Increment", action: exceeded ? restart : increment)
    }
}

编辑:

您当然可以使用经典的if/else 而不是三元运算符。 但它会破坏任何动画,如本例所示(使用 if / else,这是两个不同的按钮,动画被经典过渡替换,默认为淡入淡出进/出):

struct CounterView: View {
    @State private var counter: Int = 0
    var exceeded: Bool { counter >= 1 }
    func restart() {
        withAnimation(.linear(duration: 2)) {
            counter = 0
        }
    }
    func increment() {
        counter += 1
    }
    var body: some View {
        VStack {
            Button(exceeded ? "Restart" : "Increment", action: exceeded ? restart : increment)
                .foregroundColor(.red)
                .scaleEffect(exceeded ? 2 : 1)
            if exceeded {
                Button("Restart", action: restart)
                .scaleEffect(exceeded ? 2 : 1)
            } else {
                Button("Increment", action: increment)
                .scaleEffect(exceeded ? 2 : 1)
            }
        }
    }
}

【讨论】:

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