SwiftUI – Button – How to pass a function request to parent

The best examples on closures and how they can be used is found in the official swift documentation.

This is a small example on how to pass a closure to your child view which then calls a function of the parent:

struct ChildView: View {
    var function: () -> Void
    
    var body: some View {
        Button(action: {
            self.function()
        }, label: {
            Text("Button")
        })
    }
}

struct ParentView: View {
    var body: some View {
        ChildView(function: { self.fuctionCalledInPassedClosure() })
    }
    
    func fuctionCalledInPassedClosure() {
        print("I am the parent")
    }
}

I hope this helps!

Pass a function

And here is an example to pass the function:

struct ChildView: View {
    var function: () -> Void
    
    var body: some View {
        Button(action: {
            self.function()
        }, label: {
            Text("Button")
        })
    }
}

struct ParentView: View {
    var body: some View {
        ChildView(function: self.passedFunction)
    }
    
    func passedFunction() {
        print("I am the parent")
    }
}

Pass a function with parameters

struct ChildView: View {
    var myFunctionWithParameters: (String, Int) -> Void
    
    var body: some View {
        Button(action: {
            self.myFunctionWithParameters("parameter", 1)
        }, label: {
            Text("Button")
        })
    }
}

struct ParentView: View {
    var body: some View {
        ChildView(myFunctionWithParameters: self.passedFunction)
    }
    
    func passedFunction(myFirstParameter: String, mySecondParameter: Int) {
        print("I am the parent")
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)