【发布时间】:2021-09-16 17:11:07
【问题描述】:
在我看来,我问了用户 2 个问题。在每个问题下方,我都有一个列表,允许用户选择多个项目。
- 如何将从列表中选择的多个项目传递到下一个视图?
- 如何打印选择以在 Xcode 中调试控制台?
这是我在 Xcode 中的 Swift 代码:
import SwiftUI
struct WelcomeView: View {
@State var applications: [Application] = [Application(name: "Beer"),
Application(name: "Hard Seltzer")]
@State var legislations: [Legislation] = [Legislation(name: "Natural"),
Legislation(name: "Natural and Artificial")]
var body: some View {
NavigationView {
VStack {
Text("Which applications are you interested in?")
.padding()
List{
ForEach(0..<applications.count){ index in
HStack {
Button(action: {
applications[index].isSelected = applications[index].isSelected ? false : true
}) {
HStack{
if applications[index].isSelected {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
.animation(.easeIn)
} else {
Image(systemName: "circle")
.foregroundColor(.primary)
.animation(.easeOut)
}
Text(applications[index].name)
}
}.buttonStyle(BorderlessButtonStyle())
}
}
}
Text("Which legislations are you interested in?")
.padding()
List{
ForEach(0..<legislations.count){ index in
HStack {
Button(action: {
legislations[index].isSelected = legislations[index].isSelected ? false : true
}) {
HStack{
if legislations[index].isSelected {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
.animation(.easeIn)
} else {
Image(systemName: "circle")
.foregroundColor(.primary)
.animation(.easeOut)
}
Text(legislations[index].name)
}
}.buttonStyle(BorderlessButtonStyle())
}
}
}
NavigationLink(destination: FlavorSearchView()) {
Text("Next")
.padding()
}
}
}
}
}
struct Application: Identifiable {
var id = UUID()
var name: String
var isSelected: Bool = false
}
struct Legislation: Identifiable {
var id = UUID()
var name: String
var isSelected: Bool = false
}
struct WelcomeView_Previews: PreviewProvider {
static var previews: some View {
WelcomeView()
}
}
屏幕预览:
【问题讨论】:
-
请提供足够的代码,以便其他人更好地理解或重现问题。