gpt4 book ai didi

swift - 获取 UIViewControllerRepresentableContext 环境值?

转载 作者:行者123 更新时间:2023-12-04 16:03:52 24 4
gpt4 key购买 nike

好的,

所以这可能是微不足道的,但我不知道如何去做。

我有一个在 SwiftUI View 调用时创建的 UIViewController:

func makeUIViewController(context: Context) -> MyViewController

进行该调用的 View 在 SceneDelegate 中被赋予了一个环境对象,就像我们在教程中看到的那样:
window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(MyData()))

我想要做的是在我的 UIViewController 逻辑中使用该环境对象 (MyData())。 ViewController 会根据需要在 MyData 的实例上读/写,据我所知,这应该会导致 SwiftUI View 做出相应的 react ,因为 MyData 符合 BindableObject ...

所以在 makeUIViewController 调用中我得到了 UIViewControllerRepresentableContext。我可以在上下文中看到环境:
context.environment

如果我在调试期间在控制台中打印它,我会看到:
context.environment: [EnvironmentPropertyKey<PreferenceBridgeKey> = Value(value: Optional(SwiftUI.PreferenceBridge)), EnvironmentPropertyKey<FontKey> = Optional(SwiftUI.Font(provider: SwiftUI.(unknown context at $1c652cbec).FontBox<SwiftUI.Font.(unknown context at $1c656e2cc).TextStyleProvider>)), .......

在打印中,我看到 MyData environmentObject 实例:
EnvironmentPropertyKey<StoreKey<MyData>> = Optional(MyApp.MyData), ...

我不确定如何从 context.environment 中给我的环境值中获取 MyData ....

我试图弄清楚如何为 MyData 获取正确的 EnvironmentKey 以便我可以尝试访问它查看下标 ... context.environment[myKey ...]

如何从上下文提供给我的环境值中取回 MyData?

最佳答案

使用@EnvironmentObject 现在可以工作(但在 Xcode 预览中无效)。使用 Xcode 11.1/Swift 5.1。为简单起见,它使用了 UIViewRepresentable,但同样适用于 UIViewControllerRepresentable,因为它也是 SwiftUI View

这是完整的演示

Change @EnvironmentObject from UIButton

import SwiftUI
import Combine
import UIKit

class AppState: ObservableObject {
@Published var simpleFlag = false
}

struct CustomUIView: UIViewRepresentable {
typealias UIViewType = UIButton

@EnvironmentObject var settings: AppState

func makeUIView(context: Context) -> UIButton {
let button = UIButton(type: UIButton.ButtonType.roundedRect)
button.setTitle("Tap UIButton", for: .normal)
button.actionHandler(controlEvents: UIControl.Event.touchUpInside) {
self.settings.simpleFlag.toggle()
}
return button
}

func updateUIView(_ uiView: UIButton, context: UIViewRepresentableContext<CustomUIView>) {
}

}

struct ContentView: View {
@ObservedObject var settings: AppState = AppState()

var body: some View {
VStack(alignment: .center) {
Spacer()
CustomUIView()
.environmentObject(self.settings)
.frame(width: 100, height: 40)
.border(Color.blue)
Spacer()
if self.settings.simpleFlag {
Text("Activated").padding().background(Color.red)
}
Button(action: {
self.settings.simpleFlag.toggle()
}) {
Text("SwiftUI Button")
}
.padding()
.border(Color.blue)
}
.edgesIgnoringSafeArea(.all)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(AppState())
}
}

/// Just utility below
extension UIButton {
private func actionHandler(action:(() -> Void)? = nil) {
struct __ { static var action :(() -> Void)? }
if action != nil { __.action = action }
else { __.action?() }
}
@objc private func triggerActionHandler() {
self.actionHandler()
}
func actionHandler(controlEvents control :UIControl.Event, for action:@escaping () -> Void) {
self.actionHandler(action: action)
self.addTarget(self, action: #selector(triggerActionHandler), for: control)
}
}

关于swift - 获取 UIViewControllerRepresentableContext 环境值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56602052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com