0

我遵循了来自 git hub 的以下 Facebook 自定义登录示例

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!"];

}

视图控制器.h

@interface ViewController : UIViewController

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

视图控制器.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];
         }];
    }
}

我的模拟器屏幕

在此处输入图像描述

4

1 回答 1

1

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

// 登录

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

// 登出

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

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

于 2014-02-24T11:15:23.600 回答