Reputation: 23
I'm making a clock app, and I want to give the users choosing a several background images.(By clicking a UIButton in settingViewController, then background image will be changed in MainViewController.) So, I use segue but it doesn't work. Here is the code.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nav:UINavigationController = segue.destination as! UINavigationController
let mainVC:MainViewController = nav.visibleViewController as! MainViewController
//let mainVC: MainViewController = segue.destination as! MainViewController
if bg1Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg1.jpg")
} else if bg2Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg2.jpg")
} else if bg3Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg3.jpeg")
} else if bg4Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg4.jpg")
} else if bg5Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg5.png")
} else if bg6Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg6.png")
} else if bg7Button.isSelected == true {
mainVC.bgImg.image = UIImage(named: "bg7.jpg")
}
}
I made a breakPoint, and I realize that when it get in the 'if', it didn't check anything and just get out. I don't know why. Please give me an advice. Thank you:)
Upvotes: 1
Views: 161
Reputation: 2832
Convert the image into Data like this :-
let data = UIImagePNGRepresentation(image) as? NSData
Pass the data to nextVC :-
mainVC.imageData = data
Show image like this :-
let imagePt = UIImage(data: (imageData as! NSData) as Data)
Upvotes: 0
Reputation: 59
You convert your UIImage in NSData format and add in a dictionary or array, transfer this to MainViewController. There put NSData in UIImageView.
Upvotes: 2