My View has four buttons. When you click on a button it launches the PHPickerViewController that allows a user to select an image from their device. How can I know which button was clicked so that I can set the chosen image on that specific button?
@objc func showImage(sender: UIButton){
var configuration1 = PHPickerConfiguration(photoLibrary: .shared())
configuration1.selectionLimit = 1
configuration1.filter = .images
let picker = PHPickerViewController(configuration: configuration1)
picker.delegate = self
present(picker, animated: true, completion: nil)
}
extension ProfileController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true, completion: nil)
guard !results.isEmpty else {
return
}
for result in results {
result.itemProvider.loadObject(ofClass: UIImage.self) {
[weak self]
object, error in
DispatchQueue.main.async {
guard let self = self else {
return
}
if let image = object as? UIImage {
}
}
}
}
}
}
inside your ViewController class: var selectedButton: UIButton!
@objc func showImage(sender: UIButton) {
selectedButton = sender
var configuration1 = PHPickerConfiguration(photoLibrary: .shared())
// rest of your code
}
Then in your didFinishPicking delegate:
if let image = object as? UIImage {
selectedButton.setImage(image, for: .normal)
}