0

I have a class for my UICollectionReusableView, but I need a button in that view to perform a segue from the UICollectionView to an imagePicker. I have tried to use a delegate, but I keep on getting nil for the delegate.

protocol ShouldSegueToImagePickerDelegate{
    func shouldSegue()
}

class ProfileHeaderCollectionReusableView: UICollectionReusableView{

   var delegate: ShouldSegueToImagePickerDelegate!

   @IBAction func pressedChangeBGPicButton(sender: AnyObject) {
        delegate.shouldSegue()
    }


class ProfileCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, ShouldSegueToImagePickerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

  var imagePicker = UIImagePickerController()

  func shouldSegue() {
      imagePicker.allowsEditing = false
       imagePicker.sourceType = .PhotoLibrary
       self.presentViewController(imagePicker, animated: true, completion: nil)
   }

  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
    }

  func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
  }
user3796209
  • 449
  • 6
  • 18
  • Where did you set `ProfileHeaderCollectionReusableView` delegate in your `ProfileCollectionViewController` ? – seto nugroho May 19 '16 at 01:51
  • @setonugroho I'm not too sure what you mean. I used a XIB for my ProfileHeaderCollectionReusableView and linked it. Then in ProfileCollectionViewController, I used the method collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView ///////// let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "profileHeader", forIndexPath: indexPath) as! ProfileHeaderCollectionReusableView – user3796209 May 19 '16 at 01:54
  • Each `ProfileHeaderCollectionReusableView` instance that you created in your `collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ` has a property `delegate`. Those property is used to communicate with class that want to implement protocol `ShouldSegueToImagePickerDelegate`. If you don't set that, your delegate will be nil and and your app will crash when you run `pressedChangeBGPicButton` function since you force delegate to be non nil (hint: !). – seto nugroho May 19 '16 at 02:05

0 Answers0