我已经有一段时间了,而且它似乎没有用。我已阅读有关协议和代表但我仍然无法获取信息。我会粘贴CollectionView的确切代码,只粘贴ParentView中的相关代码,因为它有很多其他代码:
名为FBViewController的ParentViewController
类FBViewController:UIViewController,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ImageCollectionViewControllerDelegate {
var collectedImage = UIImage()
//THIS IS WHERE I COLLECT THE IMAGE FROM COLLECTION VIEW
func imageCollectionViewController(_:ImageCollectionViewController, collectedImage : UIImage) {
println("collected")
}
名为ImageCollectionViewController的集合视图控制器:
@class_protocol协议ImageCollectionViewControllerDelegate {
func imageCollectionViewController(imageCollectionViewController : ImageCollectionViewController, collectedImage: UIImage)
}
class ImageCollectionViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var images = String[]()
var selectedImage = UIImage()
let collectedImage = UIImage()
var delegate: ImageCollectionViewControllerDelegate?
@IBAction func dismissViewController() {
dismissModalViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
images = ["img1", "img2", "img3"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return images.count
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as ImageCollectionViewCell
cell.imageView.image = UIImage(named:images[indexPath.row])
return cell
}
//THIS IS WHERE I AM TRYING TO SEND THE IMAGE OVER TO FBViewController
func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) {
selectedImage = UIImage(named:images[indexPath.row])
self.delegate?.imageCollectionViewController(self, collectedImage: selectedImage)
dismissModalViewControllerAnimated(true)
}
}
任何帮助将不胜感激。感谢