I am trying to pass an image from my Collection View (PortfolioView.swift) to another View Controller (PortfolioDetail.swift). My code for PortfolioView.swift reads:
import UIKit
class Portfolio View: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var array = [String] = []
var name : AnyObject? {
get {
return NSUserDefaults.standardUserDefaults().objectForKey("name")
}
set {
NSUserDefaults.standardUserDefaults.setObject(newValue!, forKey: "name")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
override func viewDidLoad() {
super.viewDidLoad()
array = ["PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7",
"PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7",
"PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7"]
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundImage.png")!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
cell.ImageView.image = UIImage(named: array[indexPath.row])
cell.ImageView.layer.cornerRadius = cell.ImageView.frame.size.width / 2
cell.ImageView.clipsToBounds = true
cell.ImageView.layer.borderWidth = 2.0
cell.ImageView.layer.borderColor = UIColor.whiteColor().CGColor
return cell
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
name = UIImage(named: array[indexPath.row])
}
}
My PortfolioDetail.swift code reads:
import UIKit
class PortfolioDetail: UIViewController {
@IBOutlet var DetailImageView: UIImage!
@IBOutlet var DetailLabel: UILabel!
var name: AnyObject? {
get {
return NSUserDefaults.standardUserDefaults().objectForKey("name")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundImage.png")!)
DetailImageView.image = UIImage(named: name as! String)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
When I run this app I am able to get a display of my array of images but when I click on one of the images it doesn't display the image in my other VC. I think my issue is with the following line:
DetailImageView.image = UIImage(named: name as! String)