如何在 swift 中的 UITableViewCell 中创建 facebook 注销按钮



我正在使用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

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

问题

该错误指出您的 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()
}

最新更新