I have an app which sets different kind of scheduled timers, and while on iOS it works like a charm, there are issues with macOS and iPadOS. When I click on one button it and then the other, the switching works once, but on the other way around in does not. Shouldn't my current view be dismissed when I click on another Link, just like the view is being dismissed when I click on the back button on an iPhone? Here is my contentView file
clicking on a navigation button works in iOS, but not in macOS or iPadOs
The code you included has dependencies on other items not included in the code (model data, for example), and doesn't run if I just plug it into Xcode, so I can't reproduce the issue you describe. Can you reduce it to a minimal example that demonstrates the issue?
However, there is one thing in here that might be causing trouble: it looks like you are initializing some model data in view initializers. I can't be sure exactly what your initializers are doing from the code you provided, but be aware that SwiftUI can call view initializers repeatedly and at any arbitrary time. So this isn't typically a good place to do work. It's an especially bad place to create a model instance.
To instantiate model data, use either State to instantiate a value, or StateObject to instantiate an object. You apply the appropriate property wrapper to a property that you declare directly inside your view structure, which ensures the item is created exactly once. You can then pass it to child views using bindings, if necessary.
However, there is one thing in here that might be causing trouble: it looks like you are initializing some model data in view initializers. I can't be sure exactly what your initializers are doing from the code you provided, but be aware that SwiftUI can call view initializers repeatedly and at any arbitrary time. So this isn't typically a good place to do work. It's an especially bad place to create a model instance.
To instantiate model data, use either State to instantiate a value, or StateObject to instantiate an object. You apply the appropriate property wrapper to a property that you declare directly inside your view structure, which ensures the item is created exactly once. You can then pass it to child views using bindings, if necessary.
StateObject is not available to me as I am not yet on MacOs Big Sur, and I really need to use the class
But here is a woking example
I also get this message, which may help to find the cause of the problem
But here is a woking example
I also get this message, which may help to find the cause of the problem