Home > Blockchain >  iOS SwiftUI - Calling a function from previous struct
iOS SwiftUI - Calling a function from previous struct

Time:08-03

I have this view:

struct LoginButton : View {
    
    @Binding var showToastWrongLogin: Bool
    
    var body: some View{
        Button(action: {
            Task {
                await Login.checkInput()
            }
            
        }) {
            
        Text("Login")
            .padding(.vertical)
            .frame(maxWidth: .infinity)
            .foregroundColor(Color("ColorText"))
            .overlay(
                RoundedRectangle(cornerRadius: 5)
                    .stroke(Color.white, lineWidth: 0))
                
        }
        .background(Color("BtnDefaultBG"))
        .frame(minWidth: 0, maxWidth: .infinity)
        .cornerRadius(5)
        .padding(.top, 16)
    }
}

I'm trying to call the checkInput() function which is inside Login from where this view is also called

await Login.checkInput()

gives:

Instance member 'checkInput' cannot be used on type 'Login'; did you mean to use a value of this type instead?

Please help

CodePudding user response:

If I understood correctly and Login is a parent view, then you can inject function as argument here, like

struct LoginButton : View {

    @Binding var showToastWrongLogin: Bool
    let checkInput: () async -> Void         // << here !!

    var body: some View{
        Button(action: {
            Task {
                await checkInput()    // << here !!
            }

        }) {
...
  • Related