I've created a random number generator that should present a new sheet if the number 3 appears.
The logic is in a separate class but when I use .sheet
or .fullScreenCover
on the ContentView it doesn't work.
Is it possible to trigger a modal sheet from an ObservableObject in Xcode12 / iOS 14 SwiftUI?
Minimal reproducible example below:
import SwiftUI
struct ContentView: View {
@StateObject var mathLogic = MathLogic()
var body: some View {
VStack{
Text(String(mathLogic.newNumber))
.padding(.bottom, 40)
Text("Tap for a number")
.onTapGesture{
mathLogic.generateRandomNumber()
}
}
.fullScreenCover(isPresented: mathLogic.$isLucky3, content: NewModalView.init)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct NewModalView: View {
var body: some View {
Text("You hit lucky number 3!")
}
}
class MathLogic: ObservableObject {
@Published var newNumber = 0
@State var isLucky3 = false
func generateRandomNumber() {
newNumber = Int.random(in: 1..<5)
guard self.newNumber != 3 else {
// trigger modal
self.isLucky3.toggle()
return
}
}
}
The
@State
is intended to be in View, inObservableObject
we use@Published
, so it should beand to bind it via
ObservedObject
, because .$isLucky3 gives publisherTested with Xcode 12 / iOS 14