将对象传递到 UICollectionViewCell

pass an object into a UICollectionViewCell

我在 UICollectionViewCell 中使用 FBSDK 时遇到问题。我收到警告,代码将不会在 return 语句后执行。我已经在 UIViewController 中成功实现了 Facebook 登录,但是在尝试在 CollectionViewCell class 中进行时出现错误。感谢您提供的所有帮助!

class LoginCell: UICollectionViewCell, FBSDKLoginButtonDelegate {

    lazy var FBloginButton: UIButton = {
        let customButton = UIButton(type: .system)
        customButton.backgroundColor = UIColor.blue
        customButton.setTitle("Login With Facebook", for: .normal)
        customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        customButton.setTitleColor(.white, for: .normal)
        return customButton

        customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)
    }()


    // am I passing an objective from: correctly below...????
    func handleCustomFBButton() {
        FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self.delegate as! UIViewController!) { (result, err) in
            if err != nil {
                print("Custom FB Login Button Failed")
                return
            }
            self.showEmailAddress()
        }
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("did log out of facebook...")
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error != nil {
            print(error)
            return
        }
        showEmailAddress()
    }


    func showEmailAddress() {
        let accessToken = FBSDKAccessToken.current()
        guard let accessTokenString = accessToken?.tokenString else { return }

        let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
        FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
            if error != nil {
                print("Something went wrong with our FB user: ", error ?? "")
                return
            }
            print("Successfully logged in with our user: ", user ?? "")
        })

        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
            if err != nil {
                print("Failed to start graph request:", err ?? "")
                return
            }
            print(result ?? "")
        }
    }


    weak var delegate: LoginControllerDelegate?

    func handleLogin() {
        delegate?.finishLoggingIn()
    }


    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(logoImageView)
        addSubview(emailTextField)
        addSubview(passwordTextField)
        addSubview(loginButton)
        addSubview(FBloginButton)

        _ = logoImageView.anchor(centerYAnchor, left: nil, bottom: nil, right: nil, topConstant: -230, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 160, heightConstant: 160)
        logoImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

        _ = FBloginButton.anchor(loginButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 16, leftConstant: 32, bottomConstant: 0, rightConstant: 32, widthConstant: 0, heightConstant: 50)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    } 
}

你在设定目标之前返回。

尝试将 customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside) 语句放在 return customButton 语句之前。

  • 每当在暗示结束的任何函数中调用 return 该函数的实现。
  • 任何代码写入post一个return语句将不会执行.
  • 给定实现中的问题在于 lazy var FBloginButton: UIButton,post 正在设置 return 语句按钮目标。

解决问题的适当实施如下:

lazy var FBloginButton: UIButton = {
        let customButton = UIButton(type: .system)
        customButton.backgroundColor = UIColor.blue
        customButton.setTitle("Login With Facebook", for: .normal)
        customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        customButton.setTitleColor(.white, for: .normal)
        customButton.addTarget(self, action: #selector(handleCustomFBButton), for: .touchUpInside)

        return customButton
    }()