Using observableobject in a different View

Hello,

I want to use this observableobject that I made in SavingMovieView in MovieView

How do I do this...?

import SwiftUI
import CloudKit


struct MovieView: View {
    
    @State var showingSavingMovieSheet:Bool = false

    var body: some View {
        
        
        NavigationView{
            
            ZStack(alignment: .bottomTrailing){
                
                //the list of movies
                List{
                    
                  //ForEach(vm.movies) { movies in
                        
                        
                   //}
                    
                    //Your Name Block
                    NavigationLink {
                        
                        //
                        
                    } label: {
                        
                        VStack{
                            
                            Image("your name")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .cornerRadius(20)
                            
                            
                            
                            Text("Your Name")
                                .fontWeight(.bold)
                                .font(.system(size:20))
                            
                        }
                        .padding()
                        .background(Image("beige"))
                        
                        
                    }
                    //notting hill block
                    NavigationLink {
                        NottingHillView()
                    } label: {
                        VStack{
                            Image("notting hill")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .cornerRadius(20)
                            
                            
                            
                            Text("Notting Hill")
                                .fontWeight(.bold)
                                .font(.system(size:20))
                        }               .background(Image("beige"))
                            .padding()
                        
                        
                    }
                    
                    
                    //notting hill block
                    NavigationLink {
                        //Love_OtherDrugsView()
                    } label: {
                        VStack{
                            Image("love and other drugs")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .cornerRadius(20)
                            
                            
                            
                            Text("Love & Other Drugs")
                                .fontWeight(.bold)
                                .font(.system(size:20))
                        }
                        .background(Image("beige"))
                        .padding()
                        
                        
                    }
                    
                    
                }
                .scrollContentBackground(.hidden)
                
                .background(Image("beige"))
                
                
                
                Button {
                    
                    showingSavingMovieSheet.toggle()
                    
                } label: {
                    Image(systemName: "plus.circle.fill")
                        .font(.system(size: 70))
                        .foregroundColor(.accentColor)
                        .shadow(color: .gray, radius: 0.2, x: 1, y: 1)
                    
                }
                .padding()
                
                
                .sheet(isPresented: $showingSavingMovieSheet) {
                    SavingMovieView(vm: SavingMovieViewModel(container: CKContainer.default()))
                        .presentationDetents([.fraction(0.5)])
                }
                
                
            }
            .edgesIgnoringSafeArea(.bottom)
            .toolbar {
                ToolbarItem(placement: .principal){
                    Text("Movies")
                        .font(Font.custom("Titan One",size:50))
                        .bold()
                        .foregroundColor(Color.accentColor)
                    
                    
                    
                    
                }
                
            }
            
            
        }
        
        
    }
    
    
    
}


struct MovieView_Previews: PreviewProvider {
    static var previews: some View {
        MovieView()
        
    }
}


struct SavingMovieView: View {
    
    //making an object from the viewModel
    @StateObject private var vm: SavingMovieViewModel
    
    @State private var title: String = ""
    @State private var director: String = ""
    @State private var stars: String  = ""
    @State private var review: String = ""
    
    @Environment(\.dismiss) var dismiss
    
    
    init(vm: SavingMovieViewModel){
        
        _vm = StateObject(wrappedValue: vm)
        
    }
    
    
    var body: some View {
        
        
        
        VStack{
            
            Text("Add movie")
                .bold()
                .foregroundColor(Color.accentColor)
                .font(Font.custom("Titan One",size:50))
            
            
            TextField("Movie Title", text: $title)
                .textFieldStyle(.roundedBorder)
            
            
            TextField("Director Name", text: $director)
                .textFieldStyle(.roundedBorder)
            
            TextField("Number of rating stars", text: $stars)
                .textFieldStyle(.roundedBorder)
            
            TextField("Description", text: $review)
                .textFieldStyle(.roundedBorder)
            
            Button {
                vm.saveMovie(title: title, director: director, stars: stars, review: review)
                
                dismiss()
                
            } label: {
                Text("Save")
            }

        }
        .padding()
    }
    
    
    
}





struct SavingMovieView_Previews: PreviewProvider {
    static var previews: some View {
        SavingMovieView(vm: SavingMovieViewModel(container: CKContainer.default()))
        
        
        
    }
}
Answered by Claude31 in 761665022

If I understand the strcuture of your code, one way:

  • declare the StateObject in MovieView
  • pass it to the environment in the body of ModelView
  • use @EnvironmentObject var vm: SavingMovieViewModel in SavingMovieView

Could you show more code, to have a complete view and possibly test your code ?

Accepted Answer

If I understand the strcuture of your code, one way:

  • declare the StateObject in MovieView
  • pass it to the environment in the body of ModelView
  • use @EnvironmentObject var vm: SavingMovieViewModel in SavingMovieView
Using observableobject in a different View
 
 
Q