London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
-7.3 C
New York
Sunday, February 2, 2025

ios – Bind @Observable mannequin property to @ObservableObject mannequin property so view updates?


I’ve the next demo code:

import SwiftUI
import Commentary



class Car1: ObservableObject {
    @Revealed var rentDate:Date = .now
}

class Car2: ObservableObject {
    @Revealed var rentDate:Date = .now
}


@Observable class CarViewModel {
    var rentDate:Date = .now
}


struct CarView: View {
    @State var mannequin = CarViewModel()
    var physique: some View {
        Textual content("DATE: (mannequin.rentDate)")
    }
}



struct ContentView: View {
    
    @StateObject var car1 = Car1()
    @StateObject var car2 = Car2()
    @State var mannequin = CarViewModel()
    
    var physique: some View {
        
        @Bindable var mannequin = mannequin
        
        NavigationStack {
            
            VStack {
                
                Spacer()
                
                HStack {
                    Spacer()
                    Button {
                        /// I would like mannequin.rentDate to replace as car1.rentDate updates...
                        mannequin.rentDate = car1.rentDate
                        
                        //tried this:
                        /// Can not assign worth of sort 'Binding<Date>' to sort 'Date' however offers `mannequin.rentDate = $car1.rentDate`
                    } label: {
                        Textual content("Bind to Car1")
                    }
                    Spacer()
                    Button {
                        /// I would like mannequin.rentDate to replace as car2.rentDate updates...
                        mannequin.rentDate = car2.rentDate
                    } label: {
                        Textual content("Bind to Car2")
                    }
                    Spacer()
                }
                
                Spacer()
                
                HStack {
                    Spacer()
                    Button {
                        /// after I change this I would like `Textual content("DATE: (mannequin.rentDate)")` beneath to replace...
                        car1.rentDate = .now
                    } label: {
                        Textual content("Change Car1")
                    }
                    Spacer()
                    Button {
                        /// after I change this I would like `Textual content("DATE: (mannequin.rentDate)")` beneath to replace...
                        car2.rentDate = .now
                    } label: {
                        Textual content("Change Car2")
                    }
                    Spacer()
                }
                
                Spacer()
                
                Textual content("DATE: (mannequin.rentDate)")
                
                Spacer()
                
            }
            
        }

    }
    
}

#Preview {
    ContentView()
}

I would like CarViewModel rentDate to bind to both Car1 or Car2 rentDate in order that when the mannequin layer in different components of the app modifications car2.rentDate the change additionally reveals up in ContentView Textual content("DATE: (mannequin.rentDate)") and this enables me to have a view mannequin that may work with totally different dates present in different fashions…

Principally I’m making an attempt to have a strategy to bind a mannequin to observe one other mannequin. In my case the one which drives the view makes use of @Observable and the opposite fashions use ObservableObject

That is just a few demo code I wish to get working to show the performance. So I might click on on “Bind to Car1” after which tapping on “Change Car1” would set off the date on the backside to replace based mostly on car1 mannequin.rentDate. Identical for car2…

How can I get CarViewModel to observe both car1 or car2 mannequin?

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com