skip to Main Content
    .fullScreenCover(isPresented: $viewModel.showEditRoom) {
        EditRoomView(someData: someData, selectedRoom: $viewModel.selectedRoom)
    }

I have a fullScreenCover modifier that presents a view when a certain condition is met. The view that is presented, EditRoomView, takes a few parameters including selectedRoom, a binding property.

The issue I’m encountering is that when the EditRoomView is presented using fullScreenCover, it doesn’t reflect the latest value of the viewModel.selectedRoom property. I update this property when a row is selected, and then I present the EditRoomView.

I attempted to use the fullScreenCover(item: but with this approach, I encountered issues in binding a variable since selectedRoom is a Binding property in the next view. Additionally, the view becomes unresponsive after its initial appearance.

2

Answers


  1. It’s hard to infer the issues you encountered from your description. However I tried to create my View in the way you wrote and have successfully updated the selectedRoom as below. You can refer to my example to identify the differences.

    (In this example, I used List(1...5, id: .self) { room in } for simplicity and clarity. Feel free to replace it with more complex objects as needed.)

    import SwiftUI
    
    class SomeViewModel: ObservableObject {
        @Published var showEditRoom = false
        @Published var selectedRoom = 0
    }
    
    struct EditRoomView: View {
        @Environment(.presentationMode) var presentationMode
        @Binding var selectedRoom: Int
        
        var body: some View {
            Text("selectedRoom: (selectedRoom)")
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
                Text("Back")
            }
        }
    }
    
    struct ContentView: View {
        @StateObject var viewModel: SomeViewModel
        
        var body: some View {
            VStack {
                List(1...5, id: .self) { room in
                    Text("This is room: (room)")
                        .onTapGesture {
                            viewModel.selectedRoom = room
                            viewModel.showEditRoom = true
                        }
                }
            }
            .padding()
            .fullScreenCover(isPresented: $viewModel.showEditRoom) {
                EditRoomView(selectedRoom: $viewModel.selectedRoom)
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView(viewModel: SomeViewModel())
        }
    }
    
    Login or Signup to reply.
  2. Here is one implementation:

        struct Place: Identifiable, Hashable {
        let id = UUID()
        let name: String
    }
    
    struct ContentView: View {  
        
        let places = [Place(name: "Denver"), Place(name: "Houston")]
        @State private var selectedPlace: Place?
          
        var body: some View {
            List(places, id: .self, selection: $selectedPlace) { place in
                Text(place.name)
            }.fullScreenCover(item: $selectedPlace) { place in
                Text(place.name)
            }
        }
    }
    
    #Preview {
        ContentView()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search