在我的視圖中,我向用戶提出了兩個問題。在每個問題下面,我有一個串列,允許用戶選擇多個專案。
在我的視圖中,我向用戶提出了2個問題。
以下是我在Xcode中的Swift代碼:
import SwiftUI
struct WelcomeView。View {
var applications: [Application] = [Application(name: "Beer"/span>)。
Application(name: "hard Seltzer")]
var legislations: [Legislation] = [Legislation(name: "Natural") 。
Legislation(name: "Natural and Artificial")]
var body: some View {
NavigationView {
VStack {
Text("你對哪些應用程式感興趣?")
.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(apps[index].name)
}
}.buttonStyle(BorderlessButtonStyle() )
}
}
}
Text("你對哪些立法感興趣?")
.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(目的地。FlavorSearchView()) {
Text("Next"/span>)
.padding()
}
}
}
}
}
struct Application: Identifiable {
var id = UUID()
var name: String()
var isSelected: Bool = false >。
}
struct Legislation: 可識別性 {
var id = UUID()
var name: String()
var isSelected: Bool = false >。
}
struct WelcomeView_Previews: PreviewProvider {
static var previews: some View {
WelcomeView()
}
}
螢屏預覽:
uj5u.com熱心網友回復:
你可以像這樣將陣列作為引數傳遞給下一個視圖:
NavigationLink( destination: FlavorSearchView(applications: 應用, legislations: 立法)
并且在視圖的建構式中列印選擇:
struct FlavorSearchView: View {
init (applications: [Application], legislations: [Legislation]){
self.apps = applications
self.legislations = legislations
print(apps)
print(legislations)
}
let applications: [Application]。
let legislations: [Legislation]
var body。some View{
VStack{ }
}
這種方法肯定會觸發SwiftUI中已知的自動彈出的錯誤。這里討論了這個問題。 https://developer.apple.com/forums/thread/677333
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/324717.html
標籤:
上一篇:插入多對多的一張表并加入表休眠
