SwiftUI View协议中的EnvironmentObject
EnvironmentObject in protocol of SwiftUI View
我想制定这样的协议:
protocol SubscriptionManagerView: View {
var subscriptionManager: EnvironmentObject<SubscriptionManager> { get set }
}
例如在这样的视图中使用:
struct Shop: View, SubscriptionManager {
@EnvironmentObject var subscriptionManager: SubscriptionManager
第一个问题是 属性 包装器 @EnvironmentObject 无法让我响应协议 SubscriptionManagerView。
最后我想使用这样的自定义修饰符:
extension View where Self : SubscriptionManagerView {
func subscriptionManagerAlert() -> some View {
self.alert(isPresented: self.$subscriptionManager.stateShowAlert) {
Alert(title: Text(self.subscriptionManager.transactionStateTitle)
...
}))
}
}
}
有这样的想法吗?
这里是一个可能方法的演示 - 这个想法是使用视图修饰符和显式注入的订阅管理器,因此接口契约保证修饰符的客户端视图将为我们提供所需的对象。
测试 Xcode 12 / iOS 14.
class SubscriptionManager: ObservableObject {
@Published var stateShowAlert: Bool = false
@Published var transactionStateTitle = "Test"
}
struct SubscriptionManagerModifier: ViewModifier {
@ObservedObject var subscriptionManager: SubscriptionManager
func body(content: Content) -> some View {
content
.alert(isPresented: self.$subscriptionManager.stateShowAlert) {
Alert(title: Text(self.subscriptionManager.transactionStateTitle))
}
}
}
extension View {
func subscriptionManagerAlert(with subscriptionManager: SubscriptionManager) -> some View {
self.modifier(SubscriptionManagerModifier(subscriptionManager: subscriptionManager))
}
}
struct Shop: View {
@EnvironmentObject var subscriptionManager: SubscriptionManager
var body: some View {
Button("Test") {
subscriptionManager.stateShowAlert.toggle()
}
.subscriptionManagerAlert(with: subscriptionManager)
}
}
struct Shop_Previews: PreviewProvider {
static var previews: some View {
Shop().environmentObject(SubscriptionManager())
}
}
我想制定这样的协议:
protocol SubscriptionManagerView: View {
var subscriptionManager: EnvironmentObject<SubscriptionManager> { get set }
}
例如在这样的视图中使用:
struct Shop: View, SubscriptionManager {
@EnvironmentObject var subscriptionManager: SubscriptionManager
第一个问题是 属性 包装器 @EnvironmentObject 无法让我响应协议 SubscriptionManagerView。
最后我想使用这样的自定义修饰符:
extension View where Self : SubscriptionManagerView {
func subscriptionManagerAlert() -> some View {
self.alert(isPresented: self.$subscriptionManager.stateShowAlert) {
Alert(title: Text(self.subscriptionManager.transactionStateTitle)
...
}))
}
}
}
有这样的想法吗?
这里是一个可能方法的演示 - 这个想法是使用视图修饰符和显式注入的订阅管理器,因此接口契约保证修饰符的客户端视图将为我们提供所需的对象。
测试 Xcode 12 / iOS 14.
class SubscriptionManager: ObservableObject {
@Published var stateShowAlert: Bool = false
@Published var transactionStateTitle = "Test"
}
struct SubscriptionManagerModifier: ViewModifier {
@ObservedObject var subscriptionManager: SubscriptionManager
func body(content: Content) -> some View {
content
.alert(isPresented: self.$subscriptionManager.stateShowAlert) {
Alert(title: Text(self.subscriptionManager.transactionStateTitle))
}
}
}
extension View {
func subscriptionManagerAlert(with subscriptionManager: SubscriptionManager) -> some View {
self.modifier(SubscriptionManagerModifier(subscriptionManager: subscriptionManager))
}
}
struct Shop: View {
@EnvironmentObject var subscriptionManager: SubscriptionManager
var body: some View {
Button("Test") {
subscriptionManager.stateShowAlert.toggle()
}
.subscriptionManagerAlert(with: subscriptionManager)
}
}
struct Shop_Previews: PreviewProvider {
static var previews: some View {
Shop().environmentObject(SubscriptionManager())
}
}