2

我在顶部栏有登录按钮,当用户单击它时,它会登录应用程序,我希望在同一个按钮之后显示注销而不是登录,但它不会更改标题。

isCLikedLogins当单击登录时,它NSString第一次没有值,它得到 YES。

这是我正在使用的代码。

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;
}
4

3 回答 3

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;
}
于 2013-06-21T11:16:58.020 回答
1

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

我还建议使用 BOOLisClickedLogin而不是字符串来跟踪。

于 2013-06-21T11:19:31.440 回答
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
}
于 2013-06-21T11:16:53.373 回答