1

Code:

// // Login.swift // test // // // // // ContentView.swift // test // // // import SwiftUI import FirebaseAuth import FirebaseDatabase struct SignUp: View { @State var Value: String = "" @State var username: String = "" @State var email: String = "" @State var password: String = "" var body: some View { ZStack { LinearGradient(gradient: Gradient(colors: [Color.blue, Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing) .ignoresSafeArea() Rectangle() .fill(Color.white) .frame(width: 300, height: 550) .cornerRadius(10) VStack(spacing: 30){ Image(systemName: "bitcoinsign.circle") .font(.system(size: 70, weight: .medium)) .padding() TextField("Username", text: $username) .frame(width: 200, height: 30) .padding() .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0))) .foregroundColor(.green) TextField("Email", text: $email) .frame(width: 200, height: 30) .padding() .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0))) .foregroundColor(.green) TextField("Password", text: $password) .frame(width: 200, height: 30) .padding() .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.blue, style: StrokeStyle(lineWidth: 2.0))) .foregroundColor(.green) Button(action: { print("Rounded Button") }, label: { Text("Sign Up") .frame(width: 200, height: 20, alignment: .center) .padding() .background(Color.blue) .foregroundColor(Color.white) .cornerRadius(15) }) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { SignUp() } } } func signupbuttonpressed(){ createUser(withEmail: email, password: password, username: username) } func createUser(withEmail email: String, password: String, username: String){ Auth.auth().createUser(withEmail: email, password: password) { (result, error) in if let error = error { print("Failed to sign user up", error.localizedDescription) return } else{ guard let uid = result?.user.uid else { return} let values = ["email": email, "username":username] Database.database().reference().child(uid).child("users").updateChildValues(values, withCompletionBlock: { (error, ref) in if let error = error { print("failed to update databse values with error", error.localizedDescription) return } else { print("user successfully created..") } }) } } } 

When I try call the createUser(withEmail: email, password: password, username: username) function, its says Cannot find 'email' in scope. So my question is how am I able to access @State var username: String = "", @State var email: String = "" @State var password: String = "", in the signupbuttonpressed() function? I am kinda new to swift so I may be missing a simple concept, any help will be appreciated.

1 Answer 1

2

You should pass arguments inside view, like

Button(action: { // here you have access to all view's internal states createUser(withEmail: email, password: password, username: username) // << here !! }, label: { Text("Sign Up") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.