Post

Replies

Boosts

Views

Activity

isPresented: on the basis of an observed object not being empty?
I am targeting iOS17 and using @Observable. I have an array of items, and I want to present a sheet when the array is greater than zero. My @Observable looks like this: @Observable class BStoreOO { var items: [Int] = [] var showBS: Bool { items.isEmpty } func updateItems(newItems: [Int]) { self.items = newItems } } When this array gets added to, from another view, I would like to present a sheet/popover. Inside my ContentView I have a @State `... that uses that @Observable struct ContentView: View { @State var bStore = BStoreOO() Further down in my View, how should I toggle this view on the basis of the observed array not being empty? I have tried a number of ways. Some error, some don't, but none present the sheet! For example: .popover(isPresented: bStore.showBS) { PopoverContent() } Gives the error "Cannot convert value of type 'Bool' to expected argument type 'Binding'" If I add a State like this: @State private var isShowingBS = false and then add this: .onChange(of: bStore.items) { self.isShowingBS = self.bStore.items.count > 0 } I don't get errors but nothing is presented. What is the correct way to bind the presentation of the sheet to whether the observed items array is empty or not?
6
0
592
Mar ’24