【问题标题】:How to create a facebook logout button in UITableViewCell in swift如何在 UITableViewCell 中快速创建 facebook 注销按钮
【发布时间】:2017-01-19 20:13:10
【问题描述】:

我正在使用 Facebook API 登录和注销。

在我的初始视图控制器中,我放置了一个用于登录的 facebook 按钮,它起作用了。

import UIKit
import FBSDKLoginKit

class SignInViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let facebookLoginButton = FBSDKLoginButton()
        view.addSubview(facebookLoginButton)

        facebookLoginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)
        facebookLoginButton.delegate = self
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Log out!")
    }

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

        print("Success!")

        let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let desController = mainStoryboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
        self.present(desController, animated: true, completion: nil)
    }

}

在此之后,我为应用菜单创建了一个UITableViewController 在这个菜单中我创建了一个UITableViewCell 并放了一个按钮。

import UIKit
import FBSDKLoginKit

class LogOutTableViewCell: UITableViewCell, FBSDKLoginButtonDelegate {

    @IBOutlet weak var btnLogOut: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func btnLogOutAction(_ sender: UIButton) {
        print("clicked!")
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("LogOut!")
    }

}

点击这个按钮我想退出facebook。

我在LogOutTableViewCell 中有一个错误:Type LogOutTableViewCell does not conform to protocol FBSDKLoginButtonDelegate

有谁知道我如何解决这个问题?或者有人知道另一种解决方案吗?

【问题讨论】:

    标签: ios swift facebook uitableview facebook-graph-api


    【解决方案1】:

    问题

    错误表明您的 LogOutTableViewCell 不符合协议 FBSDKLoginButtonDelegate

    解决方案

    只需将loginButton(_:didCompleteWith:error:)loginButtonDidLogOut(_:) 方法添加到您的LogOutTableViewCell,以符合协议。在您的情况下,您可以将其留空,因为您在 SignInViewController 中进行了登录。

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
      // just leave it empty
    }
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
      print("did logout of facebook")
    }
    

    更新:

    因为您使用自己的@IBAction,所以您可能不需要FBSDKLoginButtonDelegate。像这样在@IBAction 中调用FBSDKLoginManager().logOut() 可能就足够了:

    @IBAction func btnLogOutAction(_ sender: UIButton) {
      print("clicked!")
      FBSDKLoginManager().logOut()
    }
    

    【讨论】:

    • 我在哪里添加它?我尝试并收到另一个错误:Use of unresolved identifier loginButton(_:didCompleteWith:error:)
    • 是的,很高兴能帮到你!
    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2012-12-30
    • 2012-05-22
    相关资源
    最近更新 更多