Relative Content

Tag Archive for swiftswiftui

How to manage ViewModels used in multple SwiftUI Views with EnvironmentObject or @StateObject and @ObservedObject

I’m a little confused on when to use @StateObject/@ObservedObject and when to use @EnvironmentObject. My confusing is for when the same ViewModel needs to be used in multiple views. For instance, the code below, I need to use SomeViewModel in all four tab views. In fact, in my current app I’m injecting all my ViewModels to the @EnvironmentObjet as shown in Method 2 below, but I recenlty ran into an issue where some UI controls were not showing and realized that by removing the calls to the view models solves the issue so, I’m now wondering if @StateObject/@ObservedObject is the right choice but after thinking about it, the way I’m showing in Method 1, I’m creating multiple instances of the SomeViewModel and got me wondering if I will end up running into different issues.

How to manage ViewModels used in multple SwiftUI Views with EnvironmentObject or @StateObject and @ObservedObject

I’m a little confused on when to use @StateObject/@ObservedObject and when to use @EnvironmentObject. My confusing is for when the same ViewModel needs to be used in multiple views. For instance, the code below, I need to use SomeViewModel in all four tab views. In fact, in my current app I’m injecting all my ViewModels to the @EnvironmentObjet as shown in Method 2 below, but I recenlty ran into an issue where some UI controls were not showing and realized that by removing the calls to the view models solves the issue so, I’m now wondering if @StateObject/@ObservedObject is the right choice but after thinking about it, the way I’m showing in Method 1, I’m creating multiple instances of the SomeViewModel and got me wondering if I will end up running into different issues.

How to manage ViewModels used in multple SwiftUI Views with EnvironmentObject or @StateObject and @ObservedObject

I’m a little confused on when to use @StateObject/@ObservedObject and when to use @EnvironmentObject. My confusing is for when the same ViewModel needs to be used in multiple views. For instance, the code below, I need to use SomeViewModel in all four tab views. In fact, in my current app I’m injecting all my ViewModels to the @EnvironmentObjet as shown in Method 2 below, but I recenlty ran into an issue where some UI controls were not showing and realized that by removing the calls to the view models solves the issue so, I’m now wondering if @StateObject/@ObservedObject is the right choice but after thinking about it, the way I’m showing in Method 1, I’m creating multiple instances of the SomeViewModel and got me wondering if I will end up running into different issues.

How to manage ViewModels used in multple SwiftUI Views with EnvironmentObject or @StateObject and @ObservedObject

I’m a little confused on when to use @StateObject/@ObservedObject and when to use @EnvironmentObject. My confusing is for when the same ViewModel needs to be used in multiple views. For instance, the code below, I need to use SomeViewModel in all four tab views. In fact, in my current app I’m injecting all my ViewModels to the @EnvironmentObjet as shown in Method 2 below, but I recenlty ran into an issue where some UI controls were not showing and realized that by removing the calls to the view models solves the issue so, I’m now wondering if @StateObject/@ObservedObject is the right choice but after thinking about it, the way I’m showing in Method 1, I’m creating multiple instances of the SomeViewModel and got me wondering if I will end up running into different issues.

Upon loading a view, how do I set the value of a property based on the value of an @AppStorage property without triggering onChange?

I have a Toggle component in my view that I want to set the value for when the view loads (based on the value of an @AppStorage property). But I also have an onChange method listening to the Toggle property that I don’t want to trigger until after the view loads. I haven’t been able to get onChange to ignore that initial property set. I’ve tried using the onAppear with flags, as well as using onInit() and accessing UserDefaults directly (which is always empy when onInit() fires). What am I doing wrong here?