【发布时间】: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