我在顶栏上有登录按钮,当用户点击它时登录应用程序登录我想要在此相同的按钮显示注销而不是登录但是它没有更改标题。
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}}
答案 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
}