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

How to access the properties of a parent function within a closure in Swift?

I’m new to Swift, and trying to do something I thought should be fairly simple.

I’m working through a class, and for the exercise code I have a view that I created called CustomButton:

import SwiftUI

struct CustomButton: View {

    // Define the variables for the label and action
    var buttonText: String
    var action: () -> Void


    var body: some View {
        // The button definition
        Button(action: {
            self.action()
        }, label: {
            Text(buttonText)
        })
        .padding()
        .border(.blue)
    }
}

CustomButton is called by the main view like this:

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

CustomButton(buttonText: "To Do", action: {
    todoList = dataService.getTodo()
    pageTitle = "To Do"
 })

This all works fine and is perfectly acceptable, but what I’d like to do is remove the redundant hardcoded pageTitle from the closure, and replace it with the buttonText variable from the outer function. Something like this:

CustomButton(buttonText: "To Do", action: {
    todoList = dataService.getTodo()
    pageTitle = CustomButton.buttonText
 })

When I try that, I get the error Instance member 'buttonText' cannot be used on type 'CustomButton'; did you mean to use a value of this type instead?

I also tried setting the pageTitle variable in the action of the CustomButton view, but that results in the error Cannot find pageTitle in scope error. I know there is a way to bind the variable between views, but I don’t know how to do that yet either. Seems like this should be pretty trivial, but it’s also been deceptively difficult to find a similar post to review in my searches online.

>Solution :

CustomButton.buttonText is unavailable since it’s a property, not a static one.

It seems like you’re having multiple CustomButton with different titles, and you want to fill in pageTitle every time a button is tapped. There isn’t data binding here because you shouldn’t make multiple buttons sharing the same pageTitle. Instead of hard coding like that, you can totally return in action closure.

    var actions: (String) -> Void

    var body: some View {
        Button {
            self.action(buttonText)
        }, label: {
            Text(buttonText)
        }
        .padding()
        .border(.blue)
    }
CustomButton(buttonText: "To Do", action: { action in
    todoList = dataService.getTodo()
    pageTitle = action
})
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