Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

iOS SwiftUI – Calling a function from previous struct

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

gives:

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

Please help

>Solution :

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 !!
            }

        }) {
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading