Home > database >  how to update variable value in SwiftUI?
how to update variable value in SwiftUI?

Time:10-14

It seems the var username and var password are not updating along with my typing in the textfield. Since I used the $ to bind, what could be the problem ?

The View:


import SwiftUI


struct RegistrationPage: View {
    
    @ObservedObject var login : LoginViewModel

  
    var body: some View {
                
    VStack{
        HStack{
        Text("username")
            TextField( "username",
                       text:  $login.username
                       )
                .autocapitalization(.none)
            
        }
            
        
            .padding()
            
        HStack{
        Text("password")
        TextField( "password",
                   text:  $login.password)
                .autocapitalization(.none)

        }
            .padding()
            
            
            
        Text("click to submit registration")
                .onTapGesture {
                    Register.insertData()
                    }
        .font(.body)
        .padding()
        }
    }
    }

The View Model


class LoginViewModel: ObservableObject {
    
    @Published var username: String = ""
    @Published var password: String = ""
    
}



The Model below (actually the server database received only blank string which I believe is the initial value of var username and var password, same issue..)



import Foundation
import LeanCloud //the BaaS service whose SDK I used in the following 
import SwiftUI


    
    struct Register {
        


        static func insertData() {
        
            do {
                
                @ObservedObject var login = LoginViewModel()


    
    let register = LCObject(className: "Register")

            
                try register.set("username", value: login.username)
                try register.set("password", value: login.password)
                

                
    _ = register.save { result in
        
        switch result {
            
        case .success:
            
            break
            
        case .failure(error: let error):

            print(error)
        }
    }
} catch {
    print(error)
}
            
        }
        
        
      
            }
            
            
        
        
        
        
    
    
    
    



CodePudding user response:

You are creating a new instance of LoginViewModel inside insertData. Because it is a new and different instance than is being used inside your view, it won't share any data.

To solve this, you can pass a reference of the same instance to the function.


struct RegistrationPage: View {
    
    @ObservedObject var login : LoginViewModel
    
    var body: some View {
        
        TextField( "username",
                   text:  $login.username
        )
            .autocapitalization(.none)
        TextField( "password",
                   text:  $login.password)
            .autocapitalization(.none)
        
        Text("click to submit registration")
            .onTapGesture {
                Register.insertData(withViewModel: login)
            }
            .font(.body)
            .padding()
    }
}


struct Register  {
    static func insertData(withViewModel login : LoginViewModel) {
        do {
            let register = LCObject(className: "Register")
            
            try register.set("username", value: login.username)
            try register.set("password", value: login.password)
            
            _ = register.save { result in
                
                switch result {
                case .success:
                    break
                case .failure(error: let error):
                    print(error)
                }
            }
        } catch {
            print(error)
        }
    }
}
  • Related