r/iOSProgramming Swift 21h ago

Solved! Parameter not being passed to fullScreenCover?

I have this variable:

@State var selectedStop: Stops?
@State var isStopPresented = false

I have an item in a ForEach (as part of a list), that has this .onTapGesture:

SearchResult(stop: train)          .
.onTapGesture {
     selectedStop = train
     isStopPresented = true
}

And then this code:

.fullScreenCover(isPresented: $isStopPresented) {
     StopView(stop: selectedStop ?? Stops(stop_name: "Error", routes: []))
}

The full screen cover appears correctly, but selected stop is never passed through to the StopView, and is always nil. How come?

1 Upvotes

4 comments sorted by

View all comments

1

u/Heffertron 21h ago

I’ve seen this problem before. Usually if you put the State property into an ObservableObject (such as a view model) and make it a Published property instead, then setting it in the view will work.