1

When I click the "Update" button, I want the text view to save the text used in the User Defaults.
I could not. Do you know how? Can you edit the codes? After updating, I want the checkbox to appear, but first I need to set user defaults.

Struct ContentView: View{
@State private var profilName = ""
@State private var profilSurname = ""

var body: some View{

 ZStack{ 

 VStack{  
            TextField("Name", text: $profilName)
                .frame(height: 60)
                .padding([.leading, .trailing], 20)
                .foregroundColor(.white)
                .font(Font.system(size: 30, design: .default))
                .multilineTextAlignment(.center)
                .accentColor(Color.white)
                .overlay(RoundedRectangle(cornerRadius:30)
                            .stroke(Color(#colorLiteral(red: 0.5411764706, green: 
0.4549019608, blue: 0.2823529412, alpha: 1)), lineWidth: 1)).padding(.horizontal, 50)

               TextField("Surname", text: $profilSurname)
                        .frame(height: 60)
                        .padding([.leading, .trailing], 20)
                        .foregroundColor(.white)
                        .font(Font.system(size: 30, design: .default))
                        .multilineTextAlignment(.center)
                        .accentColor(Color.white)
                        .overlay(RoundedRectangle(cornerRadius:30)
                                    .stroke(Color(#colorLiteral(red: 0.5411764706, green: 0.4549019608, blue: 0.2823529412, alpha: 1)), lineWidth: 1)).padding(.horizontal, 50)

 Button(action: {
             
            }, label: {
                Text("Update")
                    .frame(width:300 , height: 40)
                    .padding(10)
                    .font(Font.system(size: 30, weight: .medium, design: .serif))
                    .foregroundColor(.white)
                    .background(RoundedRectangle(cornerRadius: 45))
                    .foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))

            })
}
}
}
}

enter image description here

3 Answers 3

1

You can save textfield content by simply calling user defaults in button action

 Button(action: {
         UserDefaults.standard.set(profilSurname, forKey: "Key")
 }

To retrieve text from user defaults when app starts load user defaults data in init function like below

struct ContentView: View{
@State private var profilName = ""
@State private var profilSurname = ""

init() {
        self._profilSurname = State(initialValue: UserDefaults.standard.object(forKey: "Key") as? String ?? "")
    }

var body: some View{
Sign up to request clarification or add additional context in comments.

5 Comments

can you review my codes again. Swiftui I'm new just learning. I don't know how to do it. @Babar
your code is ok. Just add above code where button is defined, What issue are u having ?
I applied the code you gave, but the application crashes when the update presses. @Babar
updated my answer now it will not crash @metaflor22
I want the UserDefaults post to be written in textView when I close and open the application. @Babar
1
struct ContentView: View{
    @State private var profilName = ""
    @State private var profilSurname: String = UserDefaults.standard.string(forKey: "Key") ?? ""
    
    var body: some View{
        
        ZStack{
            
            VStack{
                TextField("Name", text: $profilName)
                    .frame(height: 60)
                    .padding([.leading, .trailing], 20)
                    .foregroundColor(.black)
                    .font(Font.system(size: 30, design: .default))
                    .multilineTextAlignment(.center)
                    .accentColor(Color.white)
                    .overlay(RoundedRectangle(cornerRadius:30)
                                .stroke(Color(#colorLiteral(red: 0.5411764706, green:
                                                                0.4549019608, blue: 0.2823529412, alpha: 1)), lineWidth: 1)).padding(.horizontal, 50)
                
                TextField("Surname", text: $profilSurname)
                    .frame(height: 60)
                    .padding([.leading, .trailing], 20)
                    .font(Font.system(size: 30, design: .default))
                    .multilineTextAlignment(.center)
                    .accentColor(Color.white)
                    .overlay(RoundedRectangle(cornerRadius:30)
                                .stroke(Color(#colorLiteral(red: 0.5411764706, green: 0.4549019608, blue: 0.2823529412, alpha: 1)), lineWidth: 1)).padding(.horizontal, 50)
                
                Button(action: {
                    UserDefaults.standard.set(profilSurname, forKey: "Key")
                }, label: {
                    Text("Update")
                        .frame(width:300 , height: 40)
                        .padding(10)
                        .font(Font.system(size: 30, weight: .medium, design: .serif))
                        .foregroundColor(.white)
                        .background(RoundedRectangle(cornerRadius: 45))
                        .foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))
                    
                })
                
            }
        }
    }
}

Comments

1

This will be the updated code.

Button(action: {
         // This is the action section. You can add your code here
         UserDefaults.standard.set($profilSurname.wrappedValue, forKey: "surnname")
        }, label: {
            Text("Update")
                .frame(width:300 , height: 40)
                .padding(10)
                .font(Font.system(size: 30, weight: .medium, design: .serif))
                .foregroundColor(.white)
                .background(RoundedRectangle(cornerRadius: 45))
                .foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))

        })

Getting stored values form Userdefaults

let surnName = UserDefaults.standard.object(forKey: "surnname") as? String ?? ""  

3 Comments

I applied the code you gave, but the application crashes when the update presses. @Faysal Ahmed
I want the UserDefaults post to be written in textView when I close and open the application. @Faysal Ahmed
Using this code you can get the value from Userdefaults UserDefaults.standard.object(forKey: "your_key") as? String ?? "".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.