委托即使在设定为自己之后也会返回零

时间:2018-05-01 16:26:24

标签: ios swift button delegates addtarget

我有一个ViewController,它有两个单元格的CollectionView。其中一个单元格(LoginView)有登录按钮,我想让ViewController处理。 即使委托在WelcomeViewController中设置为self,我得到的错误也是意外的。

的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 SomeDelegate {
    func didPressFacebook()
}

并且视图是这样的

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()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从我看到的代码: var delegate : LoginButtonHandlerDelegate!应为var delegate : SomeDelegate!