0

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)
        }
    }
}
2
  • 1
    Try the Apple SwiftUI Tutorials. There are a lot of different option to get this done. But with a simple glance at your code you are missing some of the basics. take the tour and read How to Ask to improve, edit and format your questions. Without a Minimal Reproducible Example it is impossible to help you troubleshoot. Commented Feb 26, 2022 at 3:47
  • Check the reply by @loremipsum, you might want to learn more about view models or state objects. Commented Feb 26, 2022 at 5:23

1 Answer 1

1

There are different options for passing data from different view. Here is an example with NotificationCenter.

Your button visible view from where you want to pass data.

struct ErstellenView: View {
    var body: some View {
        ZStack {
            Text("Some text")
            .onTapGesture {
                NotificationCenter.default.post(name: Notification.Name("Button tapped"), object: "put your data here without quotation") // Place it inside your button action
            }
        }
    }
}

The view from where you want to grab the data. You can place .oneReceive anywhere you need.

class User {
    var body: some View {
        ZStack {
            Vstack {
                 // your view design
            }
            .onReceive(NotificationCenter.default.publisher(for: Notification.Name("Button tapped")), perform: { myData in
                if let foundData = myData.object as? yourCustomDataType {
                      // Do stuff with foundData          
                }
            })
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you. is there anyway how i make it public or get it into more view because i have many different views that need the data and the erstellen view not directly link to the others
I found it easier. You can also use it in more views and that should not need to be linked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.