如何在单击第一个时间按钮后生成UIBarButtonItem Logout的标题

时间:2013-06-21 11:05:41

标签: iphone ios uibarbuttonitem

我在顶栏上有登录按钮,当用户点击它时登录应用程序登录我想要在此相同的按钮显示注销而不是登录但是它没有更改标题。

isCLikedLogins NSString第一次点击登录时没有值,为if ([appDelegate.isClickedLogin isEqualToString:@"Yes"]) { signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:@" Logout " style:UIBarButtonItemStyleBordered target:self action:@selector(loginUser)]; } else { signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:@" Login " style:UIBarButtonItemStyleBordered target:self action:@selector(loginUser)]; self.navigationItem.rightBarButtonItem =signIn_BarButton; }

以下是我正在使用的代码。

{{1}}

3 个答案:

答案 0 :(得分:2)

请尝试以下代码:

if ([appDelegate.isClickedLogin isEqualToString:@"Yes"]) 
{

  [signIn_BarButton setTitle:@"Logout"];           
}

else 
{

    signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:@" Login " style:UIBarButtonItemStyleBordered target:self action:@selector(loginUser)];            
    self.navigationItem.rightBarButtonItem =signIn_BarButton;
}

答案 1 :(得分:1)

尝试将self.navigationItem.rightBarButtonItem =signIn_BarButton;移出else语句,并确保从viewDidLoad调用此代码(如果每次都创建此视图的新实例)或viewDidAppear(如果您正在重用现有实例。)

我还建议使用BOOL跟踪isClickedLogin而不是字符串。

答案 2 :(得分:0)

您可以这样做:

·H

BOOL loginCheck;

的.m

- (void)viewWillAppear:(BOOL)animated
{
    loginCheck = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonStyleBordered target:self action:@selector(loginPressed)
}

并使用loginPressed方法:

- (void) loginPressed
{
    if (loginCheck)
    {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonStyleBordered target:self action:@selector(loginPressed)
    }
    else
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonStyleBordered target:self action:@selector(loginPressed)
    }
    loginCheck = !loginCheck
}