Yo soy un novato SwiftUI desarrollador, y estoy teniendo problemas para crear una vista de forma dinámica presenta diferentes selectores basados en una selección anterior del usuario en ese punto de vista. Para ver el problema, ejecute el código siguiente en el Simulador, cambiar el pickerOneSelection a "Selector de Tres", luego cambia de nuevo a "Selector de Dos" y el intento de cambiar la pickerTwoSelection. La aplicación se estrelle inmediatamente; el error específico con este juguete ejemplo es la "Rosca 1: EXC_BAD_ACCESS (código=1, dirección=0 x 8)". Cualquier idea es más que bienvenida!
Como un aparte, he mirado en estas preguntas: (SwiftUI jerárquica Selector de datos dinámicos se bloquea) y (swiftUi : 2 Selectores en una pantalla - bloqueo de la aplicación con "Índice fuera de rango"), pero si es posible, me gustaría evitar la conversión de mi cadena de de selección de variables (pickerOneSelection, pickerThreeSelection) en números enteros, que luego debe ser traducido en cadenas para obtener el texto asociado con la selección del usuario. Si no es posible hacer lo que yo estoy tratando de hacer sin las variables del índice, si usted me podría ayudar a entender por qué, que sería muy útil.
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
Muchas gracias por su ayuda y experiencia!