10

I need to dynamically create a Button based on some parameters

func buildButton(parameter : Parameter) -> Button {
        switch (parameter){
            case Parameter.Value1:
                return Button(
                    action: {
                        ...
                },
                    label: {
                        ...
                }
            )
            case Parameter.Value2:
                return Button(
                    action: {...},
                    label: {
                        ...
                }
            )
        }
    }

But the compiler gives me this error:

Reference to generic type 'Button' requires arguments in <...>. Insert '<<#Label: View#>>'

So if I click Fix, the function declaration becomes

func buildButton(parameter : Parameter) -> Button<Label:View>

and the compiler gives

Use of undeclared type '<#Label: View#>'

What do I need to insert here to be able to return a Button?

1

2 Answers 2

21

I'm not sure how important it is that you get a Button, but if you just need it to be displayed in another SwiftUI View without further refinements, you can just return some View. You only have to embed all your Buttons in AnyView's.

func buildButton(parameter : Parameter) -> some View {
        switch (parameter){
            case Parameter.Value1:
                return AnyView(Button(
                    action: {
                        ...
                },
                    label: {
                        ...
                })
            )
            case Parameter.Value2:
                return AnyView(Button(
                    action: {...},
                    label: {
                        ...
                })
            )
        }
    }
2
  • 2
    This is exactly what I was looking for. Comeing from a Flutter/React Native background, if I need to just return a widget/component I can declare a return type of some View in the function declaration, with a component wrapped in AnyView
    – riciloma
    Aug 2, 2019 at 9:27
  • 2
    As of Xcode 12 at least I found that I did not need to embed the Button in AnyView. Works fine without it, so code is a little cleaner.
    – Scooter
    Oct 3, 2020 at 3:42
1

If you look at the declaration of Button, you can see that it is a generic struct, so you need to supply its generic type parameter.

struct Button<Label> where Label : View

Make sure that you replace YourView with the actual type implementing View that you want to return.

class YourView: View { ... }

func buildButton(parameter : Parameter) -> Button<YourView> {
    switch (parameter){
        case Parameter.Value1:
            return Button(
                action: {
                    ...
            },
                label: {
                    ...
            }
        )
        case Parameter.Value2:
            return Button(
                action: {...},
                label: {
                    ...
            }
        )
    }
}
1
  • Thanks, I've understood. I've renamed <YourView> with the name of struct the Button is in. Now I have a different problem: if I try to insert an Image as label, label: { Image(systemName: "magnifyingglass").foregroundColor(App.Colors.NavBar.Foreground) } compiler gives error Cannot convert value of type 'some View' to closure result type 'YourView'
    – riciloma
    Aug 2, 2019 at 9:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.