Post

Replies

Boosts

Views

Activity

Reply to swiftUI Picker closes automatically
Full file for the view that has this issue occurring, I’m still new to SwiftUI so this is a learning curve. `import SwiftUI import FASwiftUI struct NewTaskView: View { @EnvironmentObject var modelData: ModelData var newTaskVM = newTaskViewModel() // vars for the NewTask section @State var taskName: String = "" @State private var taskDescription: String = "Task Description ..." @State var dueDate = Date() @State private var selectedTaskPriorityIndex = 0 @State private var selectedCategory:Int = 1 @State private var toggleDescField:Bool = false @State private var toggleCalenderField:Bool = false @State private var toggleTimeField:Bool = false let taskPriority = ["exclamationmark","exclamationmark.2","exclamationmark.3"] let taskPriorityText = ["Low", "Med", "High"] var body: some View { VStack{ Form { // Text("When is this due?") TextField("New task ...", text: $taskName) .foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/) if toggleDescField { TextEditor(text: $taskDescription) .foregroundColor(.secondary) //.animation(.easeInOut) } if toggleCalenderField { DatePicker("When is this due?", selection: $dueDate, displayedComponents: [.date, .hourAndMinute]) .datePickerStyle(GraphicalDatePickerStyle()) } // Category Picker Picker("Category:", selection: $selectedCategory) { ForEach(modelData.categories) { category in HStack { FAText(iconName: category.image, size: 12) Text(category.name) } } } Picker(selection: $selectedTaskPriorityIndex, label: Text("")) { ForEach(0 ..< taskPriority.count) { Image(systemName: "\(self.taskPriority[$0])") } } .pickerStyle(SegmentedPickerStyle()) HStack { Button(action: { toggleDescField.toggle() }){ Image(systemName: "rectangle.and.pencil.and.ellipsis") } .buttonStyle(BorderlessButtonStyle()) Button(action: { toggleCalenderField.toggle() }){ Image(systemName: "calendar.badge.clock") .padding(.leading, 5) .offset(y: 3) } .buttonStyle(BorderlessButtonStyle()) Spacer() Button(action: { let dateFormatter = ISO8601DateFormatter() //dateFormatter.formatOptions = .withFullDate let taskDate = dateFormatter.string(from: dueDate) var newTask = Task() newTask.title = taskName newTask.description = taskDescription newTask.categoryID = selectedCategory newTask.category = modelData.categories[selectedCategory].name newTask.dueDate = taskDate newTask.priority = selectedTaskPriorityIndex + 1 print(newTaskVM.saveTask(newTask: newTask)) print("Adding new task \(newTask.title)") }){ HStack { Text("Add") } } } } } Spacer() .onDisappear() { modelData.retrieveTasks() } } struct NewTaskView_Previews: PreviewProvider { static var previews: some View { NewTaskView() .environmentObject(ModelData()) } } }
Sep ’21