ric*_*oma 8 button swift swiftui
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: {
...
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
and the compiler gives
Use of undeclared type '<#Label: View#>'
What do I need to insert here to be able to return a Button?
thi*_*oxe 17
我不确定获得 Button 有多重要,但是如果您只需要将它显示在另一个 SwiftUI 视图中而无需进一步改进,则只需返回some View. 您只需将所有按钮嵌入 AnyView 中。
func buildButton(parameter : Parameter) -> some View {
switch (parameter){
case Parameter.Value1:
return AnyView(Button(
action: {
...
},
label: {
...
})
)
case Parameter.Value2:
return AnyView(Button(
action: {...},
label: {
...
})
)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5768 次 |
| 最近记录: |