SwiftUI-协调器的ObservableObject更新

时间:2019-11-29 16:19:28

标签: swift swiftui

UIViewControllerRepresentable从ViewController接收图像。该图像应可用于SwiftUI视图。

ObservableObject:

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仍然无法正常工作

我一直在努力,尝试了十几种方法。但是为了简化问题,我们有一个带有表示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"
   }
  }
 }
}...

ObservableObject:

 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方法。 (它会显示“设置字符串”),但是该值仍为“ Testing String”。

正在从控制器调用ObservableObject IS,但该值未设置或更改。

因此,该视图不会收到新值:

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")
}

1 个答案:

答案 0 :(得分:0)

这不能完全解决如何将协调器绑定到ObservableObject的问题。但是,这确实达到了预期的目标,那就是将图像从协调器返回到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
    }
   }
  }
 }
相关问题