Ketika saya mengklik tombol "Perbarui", saya ingin tampilan teks menyimpan teks yang digunakan di Default Pengguna.
Saya tidak bisa. Apa kamu tau bagaimana caranya? Bisakah Anda mengedit kode? Setelah memperbarui, saya ingin kotak centang muncul, tetapi pertama-tama saya harus mengatur default pengguna.

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

1
metaflor22 5 Januari 2021, 13:36

3 jawaban

Jawaban Terbaik

Anda dapat menyimpan konten bidang teks hanya dengan memanggil default pengguna dalam tindakan tombol

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

Untuk mengambil teks dari default pengguna saat aplikasi mulai memuat data default pengguna dalam fungsi init seperti di bawah ini

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{
0
Yodagama 5 Januari 2021, 20:08
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))
                    
                })
                
            }
        }
    }
}
1
Yodagama 5 Januari 2021, 11:30

Ini akan menjadi kode yang diperbarui.

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))

        })

Mendapatkan nilai tersimpan dari Userdefaults

let surnName = UserDefaults.standard.object(forKey: "surnname") as? String ?? ""  
1
Faysal Ahmed 5 Januari 2021, 11:31