Facebook自定义登录示例按钮名称未出现在iOS中

时间:2014-02-24 08:30:20

标签: iphone objective-c facebook uistoryboard facebook-sdk-3.1

我从git hub

跟踪了以下Facebook Custom登录示例

https://github.com/fbsamples/ios-howtos/tree/master/FBLoginCustomUISample

他们使用过XIB而不是我使用过Storyboard。

每件事情都完美无缺,但按钮名称没有出现。

如果有人知道解决此问题的解决方案,请帮助我 在此先感谢。

我的代码:

AppDelegate.M

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ViewController  *ViewObj = [[ViewController alloc]init];

    self.ViewObj = ViewObj;

    // Override point for customization after application launch.
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        NSLog(@"Found a cached session");
        // If there's one, just open the session silently, without showing the user the login UI
        [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                           allowLoginUI:NO
                                      completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                          // Handler for session state changes
                                          // This method will be called EACH time the session state changes,
                                          // also for intermediate states and NOT just when the session open
                                          [self sessionStateChanged:session state:state error:error];
                                      }];

        // If there's no cached session, we will show a login button
    } else {
        UIButton *loginButton = [self.ViewObj btn_LoginAction];
        //[loginButton setTitle:@"Log in with Facebook" forState:UIControlStateNormal];
        [loginButton setImage:[UIImage imageNamed:@"login_FB.png"] forState:UIControlStateNormal];
    }


    return YES;
}

- (void)userLoggedOut
{
    // Set the button title as "Log in with Facebook"
    UIButton *loginButton = [self.ViewObj btn_LoginAction];
   // [loginButton setTitle:@"Log in with Facebook" forState:UIControlStateNormal];
    [loginButton setImage:[UIImage imageNamed:@"login_FB.png"] forState:UIControlStateNormal];
    // Confirm logout message
    [self showMessage:@"You're now logged out" withTitle:@""];
}

// Show the user the logged-in UI
- (void)userLoggedIn
{
    // Set the button title as "Log out"
    UIButton *loginButton = self.ViewObj.btn_LoginAction;
    //[loginButton setTitle:@"Log out" forState:UIControlStateNormal];
    [loginButton setImage:[UIImage imageNamed:@"logout_FB.png"] forState:UIControlStateNormal];

    // Welcome message
    [self showMessage:@"You're now logged in" withTitle:@"Welcome!"];

}

ViewController.h

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *btn_LoginAction;

ViewController.m

- (IBAction)buttonTouched:(id)sender
{
    // If the session state is any of the two "open" states when the button is clicked
    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        [FBSession.activeSession closeAndClearTokenInformation];

        // If the session state is not any of the two "open" states when the button is clicked
    } else {
        // Open a session showing the user the login UI
        // You must ALWAYS ask for basic_info permissions when opening a session
        [FBSession openActiveSessionWithReadPermissions:@[@"basic_info,email"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {

             // Retrieve the app delegate
             AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
             // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
             [appDelegate sessionStateChanged:session state:state error:error];
         }];
    }
}

我的模拟器屏幕

enter image description here

1 个答案:

答案 0 :(得分:1)

请尝试按照以下代码设置按钮图像:

//登录

[loginButton setBackgroundImage:[UIImage imageNamed:@"login_FB.png"]
forState:UIControlStateNormal];

//退出

[loginButton setBackgroundImage:[UIImage imageNamed:@"logout_FB.png"] forState:UIControlStateNormal];

我们需要setBackgroundImage来设置按钮的标题而不是使用setImage。