2017-01-19 52 views
1

我正在使用Facebook API登录和注销。如何在swift中创建UITableViewCell中的Facebook注销按钮

在我的初始视图控制器中,我为登录添加了一个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。

我有错误:Type LogOutTableViewCell does not conform to protocol FBSDKLoginButtonDelegate

有谁知道如何解决这个问题?还是有人知道另一个解决方案吗?

回答

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

我在哪里添加它?我尝试并收到另一个错误:'使用未解析的标识符loginButton(_:didCompleteWith:error:)' –

+0

更新了我的答案@VictorMendes – ronatory

+0

谢谢!我再也没有错误了! –

相关问题