Reputation: 1
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()
}
}
}
Upvotes: 0
Views: 146
Reputation: 2680
from the code I see:
var delegate : LoginButtonHandlerDelegate!
should be var delegate : SomeDelegate!
Upvotes: 0