0

Facebookのログイン/ログアウトにUIbarbuttonItemを誘導しようとしています。ログインできますが、ログアウトできません。UIBarbuttonItemのタイトルが変更されたり、ログアウト手順が開始されたりしません。

ここにいくつかのコードがあります:

    - (void)viewDidLoad {

[super viewDidLoad];

   [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(sessionStateChanged:)
 name:FBSessionStateChangedNotification
 object:nil];

NSLog(@"Session changed");
AppDelegate *appDelegate =  (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:NO];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(sessionStateChanged:) userInfo:nil repeats:NO];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willBeginBannerViewActionNotification:) name:BannerViewActionWillBegin object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishBannerViewActionNotification:) name:BannerViewActionDidFinish object:nil];

}

    - (IBAction)authButtonAction:(id)sender {
NSLog(@"session open");
AppDelegate *appDelegate = (AppDelegate*)
[[UIApplication sharedApplication] delegate];

// If the user is authenticated, log out when the button is clicked.
// If the user is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
    [appDelegate closeSession];
} else {
    // The user has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];
}

}

    - (void)sessionStateChanged:(NSNotification*)notification {
NSLog(@"Login Activated");
self.navigationItem.leftBarButtonItem = authButton;
if (FBSession.activeSession.isOpen) {
    self.authButton =[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(sessionStateChanged:)];

}else{

    self.authButton =[[UIBarButtonItem alloc]  initWithTitle:@"Logout" style:UIBarButtonItemStyleBordered target:self action:@selector(sessionStateChanged:)];

}

}

ログレポート:

    2013-03-18 23:05:58.739 prompT[4056:907] Session changed
    2013-03-18 23:05:58.760 prompT[4056:907] 
    fb sdk error = (null)
    2013-03-18 23:05:58.763 prompT[4056:907] User session found
    2013-03-18 23:05:58.765 prompT[4056:907] Login Activated
    2013-03-18 23:05:59.268 prompT[4056:907] Login Activated
    2013-03-18 23:06:00.872 prompT[4056:907] AdAbanner failed
    2013-03-18 23:06:01.664 prompT[4056:907] Login Activated

ストーリーボードも使用しています。何かアイデアや助けはありますか?

4

1 に答える 1

1

あなたsessionStateChangedの方法にはいくつかのエラーがあります、ここにいくつかのコメントがあります

  - (void)sessionStateChanged:(NSNotification*)notification {
NSLog(@"Login Activated");

self.navigationItem.leftBarButtonItem = authButton;
       //what is this assignment doing? What is authButton? An iVar?


if (FBSession.activeSession.isOpen) {
    self.authButton =[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(sessionStateChanged:)];

  //self.authButton is not the same entity as authButton.
  //did you intend to set authButton here and then 
  //assign it to self.navigationItem.leftBarButtonItem after the conditional?       


}else{

    self.authButton =[[UIBarButtonItem alloc]  initWithTitle:@"Logout" 
               style:UIBarButtonItemStyleBordered 
               target:self action:@selector(sessionStateChanged:)];
    //is this really the action you want, or is it rather `authButtonAction`?

}

ここで実行したいのは、ボタンのタイトルを変更することだけです。

    - (void)sessionStateChanged:(NSNotification*)notification {
        NSString* buttonTitle = @"Login";
        if (FBSession.activeSession.isOpen) {
            buttonTitle = @"Logout";
        }
        self.navigationItem.leftBarButtonItem.title = buttonTitle;
    }

以前に、viewDidLoadまたはストーリーボードのいずれかにleftBarButtonItemを割り当て、初期化し、配置しておく必要があります。その際、IBActionを正しく接続する必要があります。

コードバージョン...

- (void) viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem* barButton =
        [[UIBarButtonItem alloc] initWithTitle:@"login"
                                         style:UIBarButtonItemStyleBordered
                                        target:self
                                        action:@selector(authButtonAction:)];
    self.navigationItem.leftBarButtonItem = barButton;

}
于 2013-03-19T00:29:56.947 に答える