2017-01-19 50 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

謝謝!我再也沒有錯誤了! –

相關問題