如何制作按钮导致Swiftui的其他按钮?
我的代码目前显示两个按钮彼此相邻,一个按钮是国家,另一个按钮是状态。我想做到这一点,以便一旦按下按钮,就会出现一组新的按钮。例如,如果按下国家 /地区,则两个新按钮将是美国和加拿大。如果按下州,两个新按钮将是加利福尼亚和内华达州。最终,我想提出一系列问题,每个按钮都按下确定下一个问题和选项。
我当前的代码是此代码
import SwiftUI
struct ContentView: View {
@StateObject var triviaManager = TriviaManager()
var body: some View {
NavigationView {
VStack(spacing: 40) {
VStack(spacing: 20) {
Text("Foodie's Pick")
.lilacTitle()
Text("Let's Find your Match")
.foregroundColor(Color("AccentColor"))
}
HStack { NavigationLink {
TriviaView()
.environmentObject(triviaManager)
} label: {
PrimaryButton(text: "Countries")
}
NavigationLink {
TriviaView()
.environmentObject(triviaManager)
} label: {
PrimaryButton(text: "States")
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.background(Color(red: 1, green: 1, blue: 1))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
,并且该代码在视觉上看起来像 this
My code currently shows two buttons next to each other, one button being countries and the other being states. I want to make it so that once a button is pressed, a new set of buttons will appear. For example, if countries is pressed, the two new buttons will be America and Canada. If states is pressed, the two new buttons will be California and Nevada. Ultimately I would like to make a long chain of questions where each button pressed determines the next question and options.
My current code is this
import SwiftUI
struct ContentView: View {
@StateObject var triviaManager = TriviaManager()
var body: some View {
NavigationView {
VStack(spacing: 40) {
VStack(spacing: 20) {
Text("Foodie's Pick")
.lilacTitle()
Text("Let's Find your Match")
.foregroundColor(Color("AccentColor"))
}
HStack { NavigationLink {
TriviaView()
.environmentObject(triviaManager)
} label: {
PrimaryButton(text: "Countries")
}
NavigationLink {
TriviaView()
.environmentObject(triviaManager)
} label: {
PrimaryButton(text: "States")
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.background(Color(red: 1, green: 1, blue: 1))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
and this code visually looks like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用其他{}或Switch或NavigationLink来实现此目标。
在下面的示例中,我使用了带有bool变量的其他{}使用,以确定下一步显示哪些按钮。另外,这种过程将在同一视图中显示按钮,因此比来回导航到下一个按钮可能更干净。
You can use either if else{} or switch or NavigationLink to achieve this.
In the below sample, I used if else{} with a Bool variable to determine which buttons to show next. Also, this kind of process will show the buttons in the same view, so it may be cleaner than Navigating back and forth to next buttons.