该功能在 UIViewControllerRepresentable 中不起作用

Posted

技术标签:

【中文标题】该功能在 UIViewControllerRepresentable 中不起作用【英文标题】:The function does not work in UIViewControllerRepresentable 【发布时间】:2020-01-17 07:21:40 【问题描述】:

我正在尝试通过 UIViewControllerRepresentable 在 SwiftUI 中实现 unsplash-photopicker-ios 组件。视图启动成功,但是unsplashPhotoPicker函数不起作用,总是返回nil,谁能告诉我是什么问题。

import SwiftUI
import UnsplashPhotoPicker

struct UnsplashImagePicker: UIViewControllerRepresentable 

    var urlUnsplashImage = [UnsplashPhoto]()

    let configuration = UnsplashPhotoPickerConfiguration(
      accessKey: "f99d21d6eb682196455dd29b621688aff2d525c7c3a7f95bfcb05d497f38f5dc",
      secretKey: "ccff858162e795c062ce13e9d16a2cf607076d0eb185141e35b14f589b1cd713",
      allowsMultipleSelection: false)

    func makeUIViewController(context: UIViewControllerRepresentableContext<UnsplashImagePicker>) -> UnsplashPhotoPicker 
        let unsplashImagePicker = UnsplashPhotoPicker(configuration: configuration)
        unsplashImagePicker.delegate = context.coordinator
        return unsplashImagePicker
    

    func makeCoordinator() -> UnsplashImagePicker.Coordinator 
        Coordinator(self)
    

    class Coordinator: NSObject, UnsplashPhotoPickerDelegate, UINavigationControllerDelegate 
        var parent: UnsplashImagePicker
        init(_ parent: UnsplashImagePicker) 
            self.parent = parent
        

        func unsplashPhotoPicker(_ photoPicker: UnsplashPhotoPicker, didSelectPhotos photos: [UnsplashPhoto]) 
            print(photos)
        

        func unsplashPhotoPickerDidCancel(_ photoPicker: UnsplashPhotoPicker) 
            print("Unsplash photo picker did cancel")
        
    

    func updateUIViewController(_ uiViewController: UnsplashPhotoPicker, context: UIViewControllerRepresentableContext<UnsplashImagePicker>) 

    

【问题讨论】:

【参考方案1】:

您需要从不同的 UIViewcontroller 呈现 Unsplashpicker。Unsplashpicker 不能直接用作 UIViewControllerRepresentable。

import SwiftUI 
import UIKit
import UnsplashPhotoPicker

struct UnsplashPresenter: UIViewControllerRepresentable 
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) 
//
    
    
    func makeUIViewController(context: Context) -> UIViewController 
        return UnsplashPickerVC()
    
    
    typealias UIViewControllerType = UIViewController
    



class UnsplashPickerVC: UIViewController, UnsplashPhotoPickerDelegate 
    func unsplashPhotoPicker(_ photoPicker: UnsplashPhotoPicker, didSelectPhotos photos: [UnsplashPhoto]) 
        print("=============== New Photo Selected =============== \n")
        print(photos)
    
    
    func unsplashPhotoPickerDidCancel(_ photoPicker: UnsplashPhotoPicker) 
        print("did cancel")
    
    
    var outputImage = UIImage(named: "Instructions.png")
    
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() 
        super.viewDidLoad()
        
        
        let button = UIButton()
        button.frame = CGRect(x: (self.view.frame.size.width / 2) - 25, y: (self.view.frame.size.height / 2) - 50, width: 100, height: 50)
        button.backgroundColor = UIColor.blue
        button.setTitle("Pick Images", for: .normal)
        button.addTarget(self, action: #selector(pickImages), for: .touchUpInside)
        self.view.addSubview(button)
        
        
    
    
    @objc func pickImages(sender: UIBarButtonItem) 
        let configuration = UnsplashPhotoPickerConfiguration(
          accessKey: "<Your Access Key >",
          secretKey: "<Your Secret Key >"
        )

        let photoPicker = UnsplashPhotoPicker(configuration: configuration)
        photoPicker.photoPickerDelegate = self

        present(photoPicker, animated: true, completion: nil)
    

【讨论】:

以上是关于该功能在 UIViewControllerRepresentable 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 iOS 应用程序中实现该功能?

springboot的自定义注解功能实现类该怎么写?

Android Kotlin:定期工作管理器未在后台定期运行该功能

sql server2017功能该怎么选

开玩笑地模拟 useDispatch 并在功能组件中使用该调度操作来测试参数

直到我在运行时单击屏幕,该功能才起作用