Using Textfield to make an instance

Hello

So essentially I want to create an instance of a class using the TextField input

class Movie: ObservableObject{
    
    @Published var name:String = ""
    @Published var director:String = ""
    @Published var stars:Double = 0.0
    @Published var review:String = ""
    
    func setName(theName:String){
        
        self.name = theName
    }
    
    func setDirector(theDirector:String){
        self.director = theDirector
    }
    
    func setStars(theStars:Double){
        self.stars = theStars
    }
    
    func setReview(theReview:String){
        self.review = theReview
    }
    
}

so this is the class that I want to initialise and I want to get a value from the struct below and store it in variables in this class

struct BottomSheetView:View{
    
    @StateObject var newMovie = Movie()
    
    var body: some View{

        VStack(alignment: .leading){
            
                Text("Add Movie")
                    .fontWeight(.bold)
                    .font(.system(size:30))
                    .padding()
            
            TextField("Movie name", text:  ????
            
            
                
            }
            
            
        }
    }

I want to get a String from the textfield above and store to var name:String in the Movie class

How do I do this?

Answered by workingdogintokyo in 761662022

You could use the ObservableObject directly, there is no need for anything else, it is made to be use like in this example code:


struct ContentView: View {
    var body: some View {
        BottomSheetView()
    }
}

class Movie: ObservableObject{
    @Published var name: String = ""
    @Published var director: String = ""
    @Published var stars: Double = 0.0
    @Published var review: String = ""

    // no need for all the functions
}

struct BottomSheetView:View{
    @StateObject var newMovie = Movie()
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("Add Movie")
                .fontWeight(.bold)
                .font(.system(size:30))
                .padding()
            
            TextField("Movie name", text: $newMovie.name)  // <--- here, etc...
            TextField("Movie director", text: $newMovie.director)
            
            // for testing, print to the console
            Button("show me") {
                print("\n---> newMovie: \(newMovie.name)   \(newMovie.director)  \n")
            }
        }
    }
}

I would try the following (not tested of course):

struct BottomSheetView:View{
    
    @StateObject var newMovie = Movie()
    @State var name = ""
    
    var body: some View {

        VStack(alignment: .leading) {
            
                Text("Add Movie")
                    .fontWeight(.bold)
                    .font(.system(size:30))
                    .padding()
            
            TextField("Movie name", text:  $name)
                   .onSubmit {            
                        newMovie.name = self.name
                    }
            }
            
        }
    }
Accepted Answer

You could use the ObservableObject directly, there is no need for anything else, it is made to be use like in this example code:


struct ContentView: View {
    var body: some View {
        BottomSheetView()
    }
}

class Movie: ObservableObject{
    @Published var name: String = ""
    @Published var director: String = ""
    @Published var stars: Double = 0.0
    @Published var review: String = ""

    // no need for all the functions
}

struct BottomSheetView:View{
    @StateObject var newMovie = Movie()
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("Add Movie")
                .fontWeight(.bold)
                .font(.system(size:30))
                .padding()
            
            TextField("Movie name", text: $newMovie.name)  // <--- here, etc...
            TextField("Movie director", text: $newMovie.director)
            
            // for testing, print to the console
            Button("show me") {
                print("\n---> newMovie: \(newMovie.name)   \(newMovie.director)  \n")
            }
        }
    }
}
Using Textfield to make an instance
 
 
Q