Post

Replies

Boosts

Views

Activity

infinite loop when using picker with navigationLink style and NavigationStack and EnvironmentObject
Hello, I found that using a Picker with the navigationLink picker style and a NavigationStack will result in an infinite loop / frozen app when the view that contains the picker uses an @Environment or @EnvironmentObject member variable. Has anyone else encountered this? Is there a workaround? Below is a simple example that reproduces for me on iOS 16.4. In my experience adding any @Environment or @EnvironmentObject member variable will cause this problem, even if the variable is not used for anything in the code import SwiftUI enum ItemType: CaseIterable { case apple case pear case orange case grapefuit } struct Item: Identifiable { let id = UUID() var type: ItemType } struct ContentView: View { @State var items = [Item(type: .apple), Item(type: .orange)] var body: some View { NavigationStack { List($items) { $item in NavigationLink(destination: ItemEditView(item: $item)) { Text(String(describing: $item.type.wrappedValue)) } } } } } struct ItemEditView: View { // If this line is commented out, it works fine. // If this line is present, then tapping on the picker will // cause an infinite loop and the app freezes. @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> @Binding var item: Item var body: some View { Form { Picker("Item Type", selection: $item.type) { ForEach(ItemType.allCases, id: \.self) { value in Text(String(describing: value)).tag(value) } } .pickerStyle(.navigationLink) } } }
1
1
806
Apr ’23