将对象传递给 UICollectionViewCell

Posted

技术标签:

【中文标题】将对象传递给 UICollectionViewCell【英文标题】:pass an object into a UICollectionViewCell 【发布时间】:2017-03-12 15:38:11 【问题描述】:

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

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")
     

【问题讨论】:

【参考方案1】: 每当return 在任何暗示结束的函数内部调用时 该函数的实现。 任何代码发布return 语句都不会执行。 给定实现中的问题在于lazy var FBloginButton: UIButton,发布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
    ()

【讨论】:

【参考方案2】:

你在设定目标之前就回来了。

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

【讨论】:

以上是关于将对象传递给 UICollectionViewCell的主要内容,如果未能解决你的问题,请参考以下文章

将对象传递给 UICollectionViewCell

React:将对象传递给状态

如何将对象传递给另一个数组对象

使用 NSKeyedArchiver 将对象传递给 WatchKit

将对象传递给新活动返回 null

将对象传递给 Java 中的不同库