用另一个类的参数调用viewcontroller中的一个函数

时间:2022-04-01 16:00:35

I have a class named CameraManager. It inherits from UINavigationControllerDelegate and UIImagePickerControllerDelegate.

我有一个班叫CameraManager。它继承自UINavigationControllerDelegate和UIImagePickerControllerDelegate。

In func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) I want to call setImageInImageView(image:UIImage) function that is in viewcontroller and pass the photo taked by user to it.

在func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any])中,我想调用viewcontroller中的setImageInImageView(image:UIImage)函数,并将用户接收的照片传递给它。

Here is my wrong code in CameraManager class.

这是我在CameraManager类中的错误代码。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    ViewController.setImageToImageView(theImage: chosenImage) // wrong code
}

I know I can set the photo in different ways, like create an outlet to imageview in my CameraManager class, or viewcontroller inherits UINavigationControllerDelegate and UIImagePickerControllerDelegate.

我知道我可以用不同的方式设置照片,比如在我的CameraManager类中创建一个到imageview的outlet,或者viewcontroller继承了UINavigationControllerDelegate和UIImagePickerControllerDelegate。

But I want do this in separated class. After some searches I found NotificationCenter, but it's complex for a swift beginner like me and there is few resources in swift 3 with passing parameter.

但是我想在单独的课上做这个。经过一些搜索之后,我找到了NotificationCenter,但是对于像我这样的快速初学者来说,它很复杂,并且在带有传递参数的swift 3中几乎没有资源。

1 个解决方案

#1


1  

You can pass your opened ViewController object to CameraManager class and in func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) you can openViewControllerObject.setImageToImageView(theImage: chosenImage).

您可以将打开的ViewController对象传递给CameraManager类和func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]),您可以使用openViewControllerObject。setImageToImageView(上图:chosenImage)。

#1


1  

You can pass your opened ViewController object to CameraManager class and in func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) you can openViewControllerObject.setImageToImageView(theImage: chosenImage).

您可以将打开的ViewController对象传递给CameraManager类和func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]),您可以使用openViewControllerObject。setImageToImageView(上图:chosenImage)。