Hi everyone,
I'm building my first project on Xcode using SwiftUI. While working on a screen that lets people enter the names of players. Additionally, it should and open a popup sheet that lets them delete certain players. While doing this I encountered a strange error. The Preview seems to crash every time I try to delete an entry in the list.
Does anyone have an idea how I could get the following code to work?
Thanks very much, any help is highly appreciated.
SwiftUI
import SwiftUI
struct Test_17_05_1: View {
@State var pName: [String] = [""]
@State var playerEnterCount: Int = 0
@State var showPlayerlist: Bool = false
func delete(at offsets: IndexSet) {
pName.remove(atOffsets: offsets)
}
var body: some View {
NavigationView {
VStack {
Spacer()
HStack {
Image (systemName: "person.fill")
.foregroundColor(.secondary)
Text("What's the name of player " +
String(playerEnterCount+1) + "?")
Spacer ()
}
HStack {
Image (systemName: "person.fill.badge.plus")
.foregroundColor(.secondary)
}
Spacer()
Button("next player") {
playerEnterCount += 1
pName.append("")
}
.foregroundColor(.secondary)
Spacer()
}
.navigationBarItems(trailing:
Button(action: {
showPlayerlist.toggle()
},
label: {
Image(systemName: "person.3.fill")
Text("Players")
}).foregroundColor(.secondary)
)
.sheet(isPresented: $showPlayerlist, content: {
List {ForEach(pName, id: \.self) {pName in
Text(pName)
}.onDelete(perform: delete)
}
})
}
}
}
4
0
1.2k