VC that fires perform segue
.
It has a backgroundImage
with a userImage
and a collectionView
with images in cells.
import UIKit
class EmojiCollectionVC: {
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var emojiCollection: UICollectionView!
var userImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
backgroundImage.image = userImage
}
@IBAction func dismiss(_ sender: Any) {
dismiss(animated: false, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! EmojiCollectionCell
let chosenEmoji = cell.emojiView.image
performSegue(withIdentifier: "backToEmojiVC", sender: chosenEmoji)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToEmojiVC"{
if let destinationVC = segue.destination as? EmojiVC {
if let emoji = sender as? UIImage {
destinationVC.emojiImage = emoji
let data = UIImagePNGRepresentation(userImage)
print("Zhenya: 1 - \(data)")
destinationVC.imageData = data
print("Zhenya: 5 - \(destinationVC.imageData)")
}
}
}
}
}
userImage
has an image in it that is displayed.
After pressing the cell, image from it (chosenEmoji
) should be passed to EmojiVC
, to its emojiImage
.
Both prints "Zhenya: 1" and "Zhenya: 5" in prepare for segue
print desired value.
The destination VC:
class EmojiVC: UIViewController {
@IBOutlet weak var mainImg: UIImageView!
@IBOutlet weak var emojiImageView: UIImageView!
var imageData: Data!
var imageItself: UIImage!
var emojiImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
print("Zhenya:3 - \(imageData)")
print("Zhenya:4 - \(emojiImage)")
}
override func viewDidAppear(_ animated: Bool) {
print("Zhenya:3 - \(imageData)")
print("Zhenya:4 - \(emojiImage)")
if imageData != nil {
print("Zhenya:2 - \(imageData)")
let img = UIImage(data: imageData)
mainImg.image = img
} else if imageItself != nil {
mainImg.image = imageItself
}
if emojiImage != nil {
print("Zhenya: \(emojiImage)")
emojiImageView.image = emojiImage
}
}
@IBAction func addEmoji(_ sender: Any) {
let img = mainImg.image
performSegue(withIdentifier: "EmojiCollectionVC", sender: img)
}
}
Both prints Zhenya: 3
and Zhenya: 4
print nil
. Data that was set in prepare for segue
wasn't passed to them.
Segue backToEmojiVC
is performed, but data isn't passed.
I did check - if there are any other segues with same identifiers.
I suspect, that value gets destroyed somewhere or somehow when destination VC appears. Or both imageData
and emojiImage
are re-initialized with nil
values.
What might be the problem?
Went through the code step by step. Copy pasted names of segues instead of typing them. Shit started to work.