如何在单独的函数中访问@State 变量

Posted

技术标签:

【中文标题】如何在单独的函数中访问@State 变量【英文标题】:How to access an @State variable inside a separate function 【发布时间】: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 有点陌生,所以我可能会遗漏一个简单的概念,我们将不胜感激。

【问题讨论】:

【参考方案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")

【讨论】:

以上是关于如何在单独的函数中访问@State 变量的主要内容,如果未能解决你的问题,请参考以下文章

Swift UI - 如何从外部访问 @State 变量?

如何在单独的 js 文件中使用访问 django 模板变量,而不是在同一个 html 文件中包含小脚本块?

SwiftUI - 如何创建全局 @State 变量?

React中的state是啥,它是如何使用的?

char *在一个结构中传递给函数。如何访问这些数据?

c ++:OOP:如何在单独的头文件中更新数组?