У меня есть collectionView
, где каждый cell
содержит UIImage
и пустой UIButton
. Как мне изображение, на которое пользователь нажал?
class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var imageCollectionView: UICollectionView!
let images: [UIImage] = [
UIImage(named: "beerImage")!,
UIImage(named: "christmasImage")!,
UIImage(named: "goalImage")!,
UIImage(named: "travelImage")!,
UIImage(named: "rollerImage")!,
UIImage(named: "giftImage")!,
UIImage(named: "shirtImage")!,
UIImage(named: "dressImage")!,
]
let columnLayout = FlowLayout(
itemSize: CGSize(width: 150, height: 150),
minimumInteritemSpacing: 30,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
var colViewWidth: CGFloat = 0.0
override func viewDidLoad() {
self.imageCollectionView.collectionViewLayout = columnLayout
super.viewDidLoad()
imageCollectionView.dataSource = self
imageCollectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
return cell
}
@IBAction func imageButtonTapped(_ sender: Any) {
print("tapped")
}
@IBAction func closeButtonTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! ExampleViewController
self.present(HomeViewController, animated: false, completion: nil)
}
}
Обновление
В конце я хотел бы передать это изображение другому ViewController. Я пробовал это таким образом, но изображение не отображается в другом ViewController:
ViewControllerA
var tappedImage = UIImage()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
tappedImage = images[indexPath.row]
}
var showPopUpView = true
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(showPopUpView)
var vc = segue.destination as! ExampleViewController
vc.pickedImage = self.tappedImage
vc.ShowPopUpView = self.showPopUpView
}
ViewControllerB
var pickedImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
imagePreview.image = pickedImage