EnvironmentObject 和 ObservableObject 未在预览中显示输入数据

问题描述 投票:0回答:1

我一直在尝试在我的应用程序上输入一些虚假数据,看看它是否正确保存。问题是,当我在不同的视图中输入所有数据时,似乎出现了问题,因为在我想要显示数据的最后一个视图上,我没有得到任何数据。

这是代码:

class UserInfo: ObservableObject{
    @Published var textFieldName: String
    @Published var textFieldLastName: String
    @Published var textFieldMail: String
    @Published var textFieldPhone: String
    @Published var textFieldId: String
    
    init(Name: String = "", LastName: String = "", Mail: String = "", Number: String = "", Id: String = "") {
        self.textFieldName = Name
        self.textFieldLastName = LastName
        self.textFieldMail = Mail
        self.textFieldPhone = Number
        self.textFieldId = Id
        
    }
    
}

struct ContentView: View {
    var body: some View {
        NavigationStack {
            welcomeView()
                .environmentObject(UserInfo(Name: "", LastName: "", Mail: "", Number: "", Id: ""))
        }
    }
}


struct welcomeView: View {
    var body: some View {
        NavigationStack {
            ZStack {
                Color.black
                    .ignoresSafeArea()
                
                VStack {
                    Image("llama")
                        .resizable()
                        .frame(width: 150, height: 150)
                    
                    Spacer().frame(height: 0)
                    
                    HStack {
                        NavigationLink(destination: LogginView()) {
                            ZStack {
                                            RoundedRectangle(cornerRadius: 40.0)
                                                .fill(Color(.gray))
                                            
                                            
                                            Text("Loggin")
                                                .foregroundColor(.black)
                                                .fontWeight(.semibold)
                            }
                        }
                        
                        NavigationLink(destination: CreateAccountView()) {
                            ZStack {
                                    RoundedRectangle(cornerRadius: 40.0)
                                        .fill(Color(.gray))
                                    Text("Crear Cuenta")
                                        .foregroundColor(.black)
                                        .fontWeight(.semibold)
                            }
                        }
                        
                        
                    }.frame(maxHeight: 100)
                        .offset(y: 170)
                }
            }
        }
    }
}




struct LogginView: View {
    var body: some View {
        VStack {
            Text("Loggin")
        }
    }
}

struct CreateAccountView: View {
    @EnvironmentObject private var userInfo: UserInfo
    @State private var textFieldPhone: String = ""
    var body: some View {
        NavigationStack {
            ZStack {
                VStack {
                    HStack {
                        Spacer().frame(width: 15)
                        Text("Comenzemos !")
                            .fontWeight(.bold)
                            .font(.system(size: 30))
                        Spacer()
                    }
                    
                    Spacer().frame(height: 10)
                    
                    HStack {
                        Spacer().frame(width: 15)
                        Text("Escribe tu numero de telefono. Nosotros te enviaremos un codigo de confirmacion ahi.")
                            .foregroundStyle(Color(.gray))
                        Spacer()
                    }
                    
                    Spacer().frame(height: 40)
                    
                    HStack {
                        ZStack{
                            RoundedRectangle(cornerRadius: 20)
                                .fill(Color(.white))
                                .opacity(0.12)
                                .frame(maxWidth: 100)
                                .frame(maxHeight: 60)
                            HStack {
                                Image("peru")
                                    .resizable()
                                    .frame(maxHeight: 30)
                                .frame(maxWidth: 30)
                                
                                Text("+51")
                                   
                            }
                        }
                        
                        Spacer().frame(width: 10)
                        
                        ZStack{
                            RoundedRectangle(cornerRadius: 20)
                                .fill(Color(.white))
                                .opacity(0.2)
                                .frame(maxWidth: 300)
                                .frame(maxHeight: 60)
                            HStack {
                                
                                
                                TextField("", text: $textFieldPhone, prompt: Text("Numero de telefono").foregroundStyle(Color(red: 200/255, green: 200/255, blue: 200/255)))
                                    .keyboardType(.numberPad)
                                    .padding(.leading, 20)
                                   
                            }
                        }
                    }.padding(.leading, 15)
                        .padding(.trailing, 15)

                    Spacer()
                    
                    HStack{
                        NavigationLink (destination: userDataName().environmentObject(userInfo)) {
                            ZStack{
                                HStack{
                                    RoundedRectangle(cornerRadius: 45)
                                        .foregroundStyle(Color(red: 76/255, green: 0/255, blue: 153/255))
                                        .opacity(0.7)
                                }.frame(maxHeight: 60)
                                HStack{
                                    Text("Continue")
                                        .foregroundStyle(Color(.gray))
                                        .fontWeight(.semibold)
                                }
                            }
                        }
                        .onTapGesture {
                            userInfo.textFieldPhone = textFieldPhone
                            
                        }
                    }
                }
            }
        }
    }
}



struct userDataName: View{
    @EnvironmentObject private var userInfo: UserInfo
    @State private var textFieldName: String = ""
    @State private var textFieldLastName: String = ""
    @State private var textFieldId: String = ""
    var body: some View{
        ZStack {
            Color(.black)
                .ignoresSafeArea()
            VStack{
                ZStack{
                    HStack {
                        RoundedRectangle(cornerRadius: 20)
                            .fill(Color(.white))
                            .opacity(0.12)
                            .frame(maxWidth: 250)
                        .frame(maxHeight: 60)
                        Spacer()
                    }
                    HStack {
                        TextField("", text: $textFieldName, prompt: Text("Nombre").foregroundStyle(Color(red: 200/255, green: 200/255, blue: 200/255)))
                        Spacer()
                            
                    }.padding(.leading, 15)
                }
                
                ZStack{
                    HStack {
                        RoundedRectangle(cornerRadius: 20)
                            .fill(Color(.white))
                            .opacity(0.12)
                            .frame(maxWidth: 250)
                        .frame(maxHeight: 60)
                        Spacer()
                    }
                    HStack {
                        TextField("", text: $textFieldLastName, prompt: Text("Apellido").foregroundStyle(Color(red: 200/255, green: 200/255, blue: 200/255)))
                           Spacer()
                    }.padding(.leading, 15)
                }
                
                ZStack{
                    HStack {
                        RoundedRectangle(cornerRadius: 20)
                            .fill(Color(.white))
                            .opacity(0.12)
                            .frame(maxWidth: 250)
                        .frame(maxHeight: 60)
                        Spacer()
                    }
                    HStack {
                        TextField("", text: $textFieldId, prompt: Text("Documento de identidad").foregroundStyle(Color(red: 200/255, green: 200/255, blue: 200/255)))
                           Spacer()
                    }.padding(.leading, 15)
                }
                
                
                Spacer()
                
                HStack{
                    NavigationLink(destination: emailData().environmentObject(userInfo)) {
                        ZStack{
                            HStack{
                                RoundedRectangle(cornerRadius: 45)
                                    .foregroundStyle(Color(red: 76/255, green: 0/255, blue: 153/255))
                                    .opacity(0.7)
                            }.frame(maxHeight: 60)
                            
                            HStack{
                                Text("Continue")
                                    .foregroundStyle(Color(.gray))
                                    .fontWeight(.semibold)
                            }
                        }
                    }
                    .onTapGesture {
                        userInfo.textFieldName = textFieldName
                        userInfo.textFieldLastName = textFieldLastName
                        
                    }
                }
            }
        }
    }
}

struct emailData: View{
    @EnvironmentObject private var userInfo: UserInfo
    @State private var textFieldMail: String = ""
    var body: some View{
        VStack{
            HStack {
                Spacer().frame(width: 15)
                Text("Correo Electronico")
                    .font(.system(size: 30))
                    .fontWeight(.bold)
                Spacer()
            }
            Spacer().frame(height: 10)
            HStack {
                Spacer().frame(width: 15)
                Text("Escribe tu correo electronico para asociarlo a tu cuenta ")
                    .foregroundStyle(Color(.gray))
                Spacer()
            }
            
            Spacer().frame(height: 50)
            
            ZStack{
                HStack {
                    Spacer().frame(width: 15)
                    RoundedRectangle(cornerRadius: 20)
                        .fill(Color(.white))
                        .opacity(0.2)
                        .frame(maxWidth: 350)
                    .frame(maxHeight: 60)
                    Spacer()
                }
                HStack {
                    TextField("", text: $textFieldMail, prompt: Text("Correo electronico").foregroundStyle(Color(red: 200/255, green: 200/255, blue: 200/255)))
                        .keyboardType(.numberPad)
                        .padding(.leading, 30)
                       
                }
            }
            
            Spacer()
            
            HStack{
                NavigationLink (destination: UserDataTest().environmentObject(userInfo)) {
                    ZStack{
                        HStack{
                            RoundedRectangle(cornerRadius: 45)
                                .foregroundStyle(Color(red: 76/255, green: 0/255, blue: 153/255))
                                .opacity(0.7)
                        }.frame(maxHeight: 60)
                        HStack{
                            Text("Continue")
                                .foregroundStyle(Color(.gray))
                                .fontWeight(.semibold)
                        }
                    }
                }
            }
        }
    }
}


struct UserDataTest: View {
    @EnvironmentObject private var userInfo: UserInfo
    var body: some View {
        VStack {
            Text(userInfo.textFieldName)
                .foregroundStyle(Color(.red))
            Text(userInfo.textFieldLastName)
                .foregroundStyle(Color(.red))
            Text(userInfo.textFieldMail)
                .foregroundStyle(Color(.red))
            Text(userInfo.textFieldPhone)
            Text(userInfo.textFieldId)
        }
    }
}

我对环境对象有点陌生,所以我真的很感激一些帮助。

swiftui observableobject environmentobject
1个回答
0
投票

你错过了最重要的部分,正如@workingdog所说,它使用

@StateObject
代表
UserInfo

这样做:

.environmentObject(UserInfo(Name: "", LastName: "", Mail: "", Number: "", Id: ""))

您是说每次视图重新渲染时,它都会创建一个新的 UserInfo 实例,而不是始终保持单个实例处于活动状态。应该是:

struct ContentView: View {
    @StateObject private var userInfo = UserInfo()

    var body: some View {
        NavigationStack {
            welcomeView()
                .environmentObject(userInfo)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.