2016-07-07 65 views
2

我有一個從他們的SDK的Facebook登錄按鈕,並希望改變文本。我試過這段代碼:如何在Swift中以編程方式更改Facebook按鈕的文本?

facebookButton.setTitle("my text here", forState: .Normal) 

但它不起作用。有沒有辦法做到這一點?

這是Facebook登錄按鈕的代碼是什麼樣子:

//MARK: Facebook Login 

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 
    if error == nil { 

     facebookAPILogin(FBSDKAccessToken.currentAccessToken().tokenString, completionHandler: { error in 

     if error == nil { 

      self.performSegueWithIdentifier("loginToHomeSegue", sender: self) 

     } else { 



      self.showAlertWithTitle("Sorry".localized(), message: "There was a problem connecting with Facebook".localized()) 

      print(error) 

     } 
     } 
    ) 

    } else { 

     showAlertWithTitle("Sorry".localized(), message: "There was a problem connecting with Facebook".localized()) 

     print(error.localizedDescription) 

    } 
    } 


    //Facebook Logout 

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { 
    print("User logged out") 

    } 

回答

4

我才發現,逛很多錯誤的答案後,它是:

let buttonText = NSAttributedString(string: "your text here") 
facebookButton.setAttributedTitle(buttonText, for: .normal) 
+0

我目前使用Objective-C,但概念是現貨。我一直在尋找FBSDKLoginButton和FBSDKButton的來源。 FBSDKLoginButton只增加了一些屬性,並擴展了FBSDKButton。 FBSDKButton不添加任何新的屬性或函數並擴展UIButton。所以,對於FBSDKLoginButton來說,UIButton的所有概念都存在。這對我來說應該是顯而易見的,但有時候最簡單的概念需要展現出來。謝謝! – Armand

+0

不能在電視上工作 – reggie

-1

假設你在一些VC和工作的loginButton屬性是FBSDKLoginButton

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [self.loginButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"test"] 
           forState:UIControlStateNormal]; 

} 
+0

@Andreza Cristina da Silva的回答有什麼不同? –

相關問題