i am building a small swiftui social media app for school. my question is how to i get the user data (i made an class which creats an object called username with his data like username) which is created in an different view calls erstellen (german for sign up) to the other view to for example display the username in the profil. (AppInformation are the informations that i need to switch between the different views by tapping a button)
struct ErstellenView: View {
@State var isSecured: Bool = true
@State var password: String = ""
@State var username: String = ""
@State var name: String = ""
@State var email: String = ""
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var appInfo: AppInformation
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 25)
.frame(width: 380, height: 650)
.overlay(Text("Erstelle Accout ")
.foregroundColor(.white)
.frame(width: 360, height: 510, alignment: .top)
.font(.headline))
ZStack {
Button(" ") {
print("back")
presentationMode.wrappedValue.dismiss()
}
.padding(0)
Image(systemName: "chevron.down")
.foregroundColor(.white)
.padding(0)
}
.frame(width: 370, height: 630, alignment: .top)
VStack {
TextField("Username", text: $username)
.frame(width:180 ,height:40 ,alignment: .center)
.multilineTextAlignment(.center)
.font(.headline)
.background(Color.gray.opacity(0.25))
.clipShape(Capsule())
.foregroundColor(.white)
Spacer()
.frame(height: 35)
TextField("Full name", text: $name)
.frame(width:180 ,height:40 ,alignment: .center)
.multilineTextAlignment(.center)
.font(.headline)
.background(Color.gray.opacity(0.25))
.clipShape(Capsule())
.foregroundColor(.white)
Spacer()
.frame(height: 35)
TextField("Email", text: $email)
.frame(width:180 ,height:40 ,alignment: .center)
.multilineTextAlignment(.center)
.font(.headline)
.background(Color.gray.opacity(0.25))
.clipShape(Capsule())
.foregroundColor(.white)
Spacer()
.frame(height: 35)
HStack {
Spacer()
.frame(width: 37)
if isSecured {
SecureField("Password", text: $password)
.frame(width:180 ,height:40 ,alignment: .center)
.multilineTextAlignment(.center)
.font(.headline)
.background(Color.gray.opacity(0.25))
.clipShape(Capsule())
.foregroundColor(.white)
} else {
TextField("Password", text: $password)
.frame(width:180 ,height:40 ,alignment: .center)
.multilineTextAlignment(.center)
.font(.headline)
.background(Color.gray.opacity(0.25))
.clipShape(Capsule())
.foregroundColor(.white)
}
ZStack {
Button(" ") {
isSecured.toggle()
print(isSecured)
}
Image(systemName: self.isSecured ? "eye" : "eye.slash")
.foregroundColor(.gray)
}
}
Spacer()
.frame(height: 60)
Button("Erstellen"){
let appUser = User.init(username, password, email)
appInfo.finished = true
print(appUser.username)
}
.font(.headline)
.frame(width: 150, height: 50)
.background(.blue)
.foregroundColor(.white)
.clipShape(Capsule())
}
}
}
}
class User {
@Published var username: String = ""
var password: String = ""
@Published var email: String = ""
@Published var beschreibung: String = ""
init(_ username: String, _ password: String, _ email: String) {
self.username = username
self.password = password
self.email = email
self.beschreibung = beschreibung
}
}
@EnvironmentObject var appInfo: AppInformation
var body: some View {
ZStack {
if appInfo.finished {
if appInfo.home {
HomeView()
}
else if appInfo.camera {
MakePostView()
}
else if appInfo.friends {
}
else if appInfo.profil {
ProfileView()
}
if appInfo.showBar {
NavigationBar()
}
}
else {
ErstellenView().environmentObject(appInfo)
}
}
}