Sono un novizio SwiftUI sviluppatore, e sto avendo problemi a creare una vista che dinamicamente si presenta selettori diversi, basato su un precedente dell'utente di selezione in vista. Per vedere il problema, eseguire il codice riportato di seguito nel Simulatore, modificare il pickerOneSelection di "Selettore a Tre", poi cambiare di nuovo a "Selettore Due" e il tentativo di cambiare il pickerTwoSelection. L'applicazione sarà immediatamente in crash; l'errore specifico con questo giocattolo esempio è "Filettatura 1: EXC_BAD_ACCESS (codice=1, address=0 x 8)". Ogni intuizione è la maggior parte del benvenuto!
Come un a parte, ho guardato a queste domande: (SwiftUI gerarchica Selettore dinamica di dati si blocca) e (swiftUi : 2 Raccoglitori su uno schermo - app crash con "Index out of range"), ma, se possibile, vorrei evitare di convertire la stringa di selezione di variabili (pickerOneSelection, pickerThreeSelection) in numeri interi, che deve poi essere tradotti in stringhe per ottenere il testo associato con la selezione dell'utente. Se non è possibile fare ciò che sto cercando di fare a meno dell'indice di variabili, se si mi potrebbe aiutare a capire perché, che sarebbe molto utile.
import SwiftUI
struct ContentView: View {
@State var pickerOneSelection: String = "Picker Two"
@State var pickerTwoVisible: Bool = false
@State var pickerTwoSelection: Int = 3
@State var pickerThreeSelection: String = "Option 1"
var pickerOneOptions = ["Picker Two", "Picker Three"]
var pickerThreeOptions = ["Option 1", "Option 2", "Option 3"]
var body: some View {
NavigationView {
Form {
Picker("Picker One selection", selection: $pickerOneSelection) {
ForEach(pickerOneOptions, id: \.self) {
Text($0)
}
} // End of Picker 1
if pickerOneSelection == "Picker Two" {
HStack {
Text("Picker Two selection")
Spacer()
Button(String(pickerTwoSelection)) {
self.pickerTwoVisible.toggle()
}
} // End of HStack
if pickerTwoVisible {
Picker("Picker Two", selection: $pickerTwoSelection) {
ForEach(0..<32, id: \.self) {
Text(String($0))
}
}
.pickerStyle(WheelPickerStyle())
} // End of pickerTwoVisible
} else {
Picker("Picker Three selection", selection: $pickerThreeSelection) {
ForEach(pickerThreeOptions, id: \.self) {
Text($0)
}
} // End of Picker 3
}
} // End of Form
} // End of NavigationView
} // End of body
} // End of ContentView
Molte grazie per il vostro aiuto e la competenza!