假设我想要创建一个属性,以便它可以直接传递给alert(_:isPresented:actions:message:)修饰符的alert(_:isPresented:actions:message:)参数,并且可以容纳多个按钮。我会用哪种类型来声明该财产?
Button<Text>可以工作,但我希望它能容纳多个按钮。
发布于 2022-08-05 20:53:20
如果它不是单个视图,那么它应该是视图生成器生成一些视图(因为我们讨论泛型),如下面的演示所示。
struct DemoView<Content, Actions>: View where Content: View, Actions: View {
@Binding var showAlert: Bool
var actions: () -> Actions // << here !!
var content: () -> Content
init(isPresented: Binding<Bool>, @ViewBuilder actions: @escaping () -> Actions, @ViewBuilder content: @escaping () -> Content) {
self._showAlert = isPresented
self.actions = actions // << here !!
self.content = content
}
var body: some View {
content()
.alert("Alet", isPresented: $showAlert, actions: actions)
}
}用Xcode 13.4 / iOS 15.5测试
https://stackoverflow.com/questions/73254902
复制相似问题