繁体   English   中英

SwiftUI UIViewControllerRepresentable,Coordinator(self)是什么意思?

[英]SwiftUI UIViewControllerRepresentable, what does Coordinator(self) mean?

Coordinator(self) 在制作 UIViewControllerRepresentable 时意味着什么(在 SwiftUI 中使用 UIKit 组件)? 我对“自我”代表什么有点困惑。

import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    @Binding var image: UIImage?
    @Environment(\.presentationMode) var presentationMode

    class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        var parent: ImagePicker
        
        init(_ parent: ImagePicker) {
            self.parent = parent
        }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            if let uiImage = info[.originalImage] as? UIImage {
                parent.image = uiImage
            }
            
            parent.presentationMode.wrappedValue.dismiss()
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        return picker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
        
    }
}

self始终表示“当前实例”。 所以当你写...

struct ImagePicker: UIViewControllerRepresentable {
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}

... self表示“此 ImagePicker 实例”。

您之所以这样说是因为声明 Coordinator 的初始化程序的方式:

class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var parent: ImagePicker
    init(_ parent: ImagePicker) {
        self.parent = parent
    }
}

协调器需要parent级才能初始化; 正在初始化一个 Coordinator 并告诉parent级应该是谁,即(即self ,这个 ImagePicker)。

如果这些概念给您带来麻烦,您可能需要研究什么是类型和实例(面向对象编程)和/或 Swift 初始化器和初始化是如何表达的。

Coordinator(self)是一个不幸的错误,在很多例子中都是如此。 self表示在 SwiftUI 更新后立即被丢弃的结构,因此协调器 object 坚持它是没有意义的。 任何 function 调用 self 都不会做任何事情。 使用Coordinator器的正确方法显示在 Apple 的Fruta 示例PaymentButton.swift 中,您的代码看起来像这样:

import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    @Binding var image: UIImage?
    @Environment(\.presentationMode) var presentationMode


    func makeCoordinator() -> Coordinator {
        Coordinator()
    }

    func makeUIViewController(context: Context) -> UIImagePickerController {
        context.coordinator.imagePicker       
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
        context.coordinator.didPickImage = { image in
            self.image = image
        }
    }

    class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
        lazy var imagePicker: UIImagePickerController =  {
            let picker = UIImagePickerController()
            picker.delegate = self
            return picker
        }()
        
        var didPickImage: ((Void) -> UIImage)?

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            if let uiImage = info[.originalImage] as? UIImage {
                didPickImage?(uiImage)
            }
        }
    }
    

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM