![](/img/trans.png)
[英]SwiftUI - ObservableObject that inherits from NSObject does not update in iOS 13
[英]SwiftUI - ObservableObject Update from Coordinator
UIViewControllerRepresentable 从 ViewController 接收图像。 此图像应可用于 SwiftUI 视图。
可观察对象:
class CurrentImage: ObservableObject {
var didChange = PassthroughSubject<Void, Never>()
@Published var photoTaken: UIImage? = nil {didSet{didChange.send()}}
}
UIViewControllerRepresentable:
struct CameraPreviewView: UIViewControllerRepresentable {
var currentImage: CurrentImage
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
// Notification receives a copy of the image taken from the ViewController:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
if let image = notification.userInfo?["image"] as? UIImage {
// *****ERROR: self.parent.videoStatus.photoTaken is not accessible
self.parent.currentImage.photoTaken = image
}
}
}
}
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraPreviewView>) -> CameraViewController {
let cameraViewController = CameraViewController()
return cameraViewController
}
// View is not updating so we don't need this
func updateUIViewController(_ cameraViewController: CameraViewController, context: UIViewControllerRepresentableContext<CameraPreviewView>) {
}
}
ObservableObject 是“不可访问的”,因此我无法从协调器设置其中的图像。 我在这里忽略了一些简单的事情吗?
更新 2:将值传递给 ObservableObject 仍然不起作用
我一直在努力解决这个问题,并尝试了十几种方法组合。 但是为了简化问题,我们有一个 Coordinator,它带有一个表示 ObservableObject 的 var。 在这种情况下,我将保持简单并尝试设置字符串的值。
协调员:
struct CameraPreviewView: UIViewControllerRepresentable {
var cameraPhoto = CurrentPhoto()
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
print("notice received")
self.parent.cameraPhoto.testString = "This changed"
}
}
}
}...
可观察对象:
class CurrentPhoto: ObservableObject {
@Published var testString = "Testing string"
}
为了测试这一点,我还尝试了以下内容:
class CurrentPhoto: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var testString = "Testing String" {
willSet {
print("set string")
objectWillChange.send()
}
}
}
在第二个示例中,会调用 willSet 方法。 (它打印“设置字符串”)但是值仍然是“测试字符串”。
ObservableObject 正在从控制器调用,但值没有设置或更改。
因此视图不会收到新值:
struct ContentView: View {
@ObservedObject var cameraState = CurrentPhoto()
var body: some View {
VStack{
Text("test this \(cameraState.testString)")
...
注意:将值从 VIEW 传递给 ObservableObject 是可行的,因此问题以某种方式与协调器如何将值传递给 ObservedObject 相关。
以下工作并将值从视图传递到 OO:
Button(action : {
self.cameraState.testString = "from view"
}, label : {
Text("Change string")
}
这并不能完全解决如何将 Coordinator 绑定到 ObservableObject 的问题。 但是,这确实实现了预期目标,即将图像从 Coordinator 返回到 SwiftUI。
风景:
struct ContentView: View {
@State private var photoInView: Image?
@State var isRecording = true
var body: some View {
VStack{
photoInView?
.resizable()
.scaledToFit()
ZStack (alignment: .bottom) {
// Start up the CameraPreview and this will 2-way bind.
CameraPreviewView(isRecording: $isRecording, photoReceivedFromCoordinator: $photoInView)
....
然后在 UIViewRepresentable 中:
struct CameraPreviewView: UIViewControllerRepresentable {
@Binding var isRecording: Bool
@Binding var photoReceivedFromCoordinator: Image?
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
// Setting the coordinator to parent allows the coordinator to use the @Binding
let parent: CameraPreviewView
init(_ cameraPreviewController: CameraPreviewView) {
self.parent = cameraPreviewController
super.init()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "imageDidChange"), object: nil, queue: OperationQueue.main){(notification) in
if let uiImage = notification.userInfo?["image"] as? UIImage {
// We need an Image not a UI Image, so first we convert it
let image = Image(uiImage: uiImage)
// Assign the value to the parent's binding value
self.parent.photoReceivedFromCoordinator = image
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.