【问题标题】:How to access an @State variable inside a separate function如何在单独的函数中访问@State 变量
【发布时间】:2021-10-04 04:37:52
【问题描述】:

代码:

//
//  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..")
            }
            
            
            
            
            
        })
    }
    
}

}

当我尝试调用 createUser(withEmail: email, password: password, username: username) 函数时,它显示在范围内找不到“电子邮件”。所以我的问题是我如何能够访问@State var username: String = "", @State var email: String = "" @State var password: String = "",在 signupbuttonpressed() 函数中?我对 swift 有点陌生,所以我可能会遗漏一个简单的概念,我们将不胜感激。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    你应该在视图中传递参数,比如

    Button(action: {
      // here you have access to all view's internal states
      createUser(withEmail: email, password: password, username: username) // << here !!
    }, label: {
      Text("Sign Up")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多