单击另一个按钮时如何更改一个按钮的操作 (SwiftUI)?

How do I change one button's action when another is clicked (SwiftUI)?

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

换句话说,当按钮没有名称时,如何引用按钮并修改其属性?

'''swift

           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。当 counter 到达 10 时,它会显示 'restart' 按钮。

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

结果:

您可以根据 counter 变量的值改变 labelButtonaction(如果使用 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,这是两个不同的按钮,动画被替换为经典过渡,默认淡入淡出 in/out) :

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