Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

button

swiftui

How can I have a button perform an action which triggers a function in its 'parent' view? I'm trying to refactor my code so that components are as small as possible.

In this case, the button performs a few tasks, but one of them is to run a function:

Button(
 action: {
  self.setViewBackToNil()
 }){
  Text("Button")
}

// which triggers a function
func setViewBackToNil(){
 self.userData.image = nil
 self.isProcessing = false
 .... etc
}

Now, if I turn the button into its own view, I can't pass self.setViewBackToNil because it's contained within the struct of the parent.

Is there a way for a component to trigger a function within its parent?

like image 650
dot3 Avatar asked Jan 22 '20 13:01

dot3


1 Answers

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!

EDIT Additional Information

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")
    }
}
like image 59
krjw Avatar answered Oct 04 '22 03:10

krjw