首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIBarbuttonItem不变

UIBarbuttonItem不变
EN

Stack Overflow用户
提问于 2013-03-19 07:34:33
回答 1查看 144关注 0票数 0

我正在尝试使用UIbarbuttonItem来登录/注销Facebook。我可以登录,但无法注销,UIBarbuttonItem标题未更改或启动注销程序。

下面是一些代码:

代码语言:javascript
复制
    - (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];

}

代码语言:javascript
复制
    - (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];
}

}

代码语言:javascript
复制
    - (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:)];

}

}

日志报告:

代码语言:javascript
复制
    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

我也在使用故事板。有什么想法和帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-19 08:29:57

您的sessionStateChanged方法有一些错误,下面是一些注释

代码语言:javascript
复制
  - (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`?

}

这里要做的就是更改按钮的标题:

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

你应该已经在viewDidLoad或故事板中预先分配、初始化并放置了你的leftBarButtonItem。当您这样做时,您应该正确地挂接IBAction。

代码版本...

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

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15488924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档