0

I have a ViewController that has a CollectionView of two cells. One of the cells (LoginView) has the login buttons, which I want the ViewController to handle. The error I'm getting is an unexpected nil even though delegate is set to self in WelcomeViewController.

ViewController

    class WelcomeViewController: UIViewController, SomeDelegate ... {

            func createCollectionView(){
                 let loginView = LoginView()
                 loginView.delegate = self

                 //collectionView related stuff here
                 ...
            }
        ...
            override func viewDidLoad() {
                 super.viewDidLoad()
                 createLayer()
                 createCollectionView()
            }
        ...
            func didPressFacebook() {
                print("pressed")
            }
    }

Protocol looks something like this

protocol SomeDelegate {
    func didPressFacebook()
}

and the view is something like this

class LoginView: UICollectionViewCell, ConfigurableCell {

    var reuseId: String {
        get {
            return "loginView"
        }
    }

    var delegate : LoginButtonHandlerDelegate!

    //Create other view related stuff here
    ...

    let facebookButton = SocialButton(buttonType: .facebook)
    let googleButton = SocialButton(buttonType: .google)

    override init(frame: CGRect) {
        super.init(frame: frame)
        facebookButton.addTarget(self, action: #selector(didPressLogin), for: .touchUpInside)

       //Add subviews and other relatedstuff here
        ...
    }

    ...

    @objc func didPressLogin(){
        if (facebookButton.buttonIs == "facebook") {
            delegate.didPressFacebook()
        }
    }
}
Qoarth
  • 1
  • 1
  • 2
  • 1
    Why is `delegate` implicitly unwrapped? It should be optional and weak. – rmaddy May 01 '18 at 16:29
  • Well, `LoginView` is a cell. How do you create a cell from `viewDidLoad` to be later used by a collection view? A would assume you are actually creating a whole different view in a collection view delegate method that does not have a `LoginButtonHandlerDelegate` actually assigned. – Sulthan May 01 '18 at 16:33
  • Also `(facebookButton.buttonIs == "facebook")` condition cannot work correctly because it it will be always true. It would be better to just have a separate handler method for every button or just check the `sender` parameter. – Sulthan May 01 '18 at 16:34
  • This code seems to be buggy, post the code what you have done till now. – Ankit Jayaswal May 01 '18 at 16:41
  • My problem isn't that it crashes the app but it doesn't respond the didPressFacebook(). @Sulthan I am creating a whole new view and set it as a cell in collectionView and then I set the collectionView as a subview of WelcomeViewController. I know it always returns true since I haven't implemented other functionalities yet, it is just to try out if my implementatind works. – Qoarth May 01 '18 at 16:44
  • `let loginView = LoginView()` combined with the fact that `LoginView` is a `UICollectionViewCell`, don't do that. And set the delegate in cellForItemAtIndexPath – Larme May 01 '18 at 16:59
  • @Larme thank you I think I got how to work with it better. Your answer cleared things up for me. I was setting the delegate for another LoginView instance and not the cell. – Qoarth May 01 '18 at 18:21

1 Answers1

0

from the code I see: var delegate : LoginButtonHandlerDelegate! should be var delegate : SomeDelegate!

Aaron Halvorsen
  • 2,610
  • 1
  • 23
  • 31