why does the state reset to null after assignment?

The issue is due to sheet is created before button clicked. The solution for your scenario is to use .sheet(item: approach.

Here is corrected code. Tested with Xcode 12.1 / iOS 14.1

struct ContentView: View {
    
    class Object: Identifiable {
        let id: String
        init(_ id: String) { self.id = id }
    }
    
    let collection = [Object("1"),Object("2"),Object("3")]
    
    @State var state: Object?
    
    var body: some View {
        
        List(collection) { item in
            
            HStack{ Text(item.id); Spacer() }
                .background(Color.white)
                .onTapGesture(count:1, perform: {
                    self.state = item
            })
        }
        .sheet(item: $state) { item in
                 Text(item.id)
        }
    }
}

backup

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top