切换UIBarButton的可见性

时间:2013-08-16 04:15:41

标签: ios objective-c uibarbuttonitem uibarbuttonitemstyle

我创建了一个具有视图,tableview和文本视图的应用程序,我已经设置了我的应用程序,当用户单击按钮登录视图时,它会将它们带到tableview并单击表视图中的单元格将他们带到textview。我只使用了单个xib用于所有这些。一切正常。现在,当我导航到表视图时,UIBarButton有3个按钮,返回视图(左),返回表视图(右)和清除按钮(右),当我在表格视图中单击一个单元格时,它进入文本视图和所有3个按钮仍然存在。我需要的是当tableview出现时,应该隐藏按钮返回日志,其他2应该是可见的,当我在textview上时,应该可以看到返回主页和返回日志,并且应该隐藏清除按钮。有没有办法实现这个?这些是我的UIBarButtons代码:

UIBarButtonItem *clearHistory=[[UIBarButtonItem alloc]initWithTitle:@"Clear History" style:UIBarButtonItemStyleBordered target:self action:@selector(clearTable)];

    UIBarButtonItem *btnBack=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem=btnBack;

    UIBarButtonItem *btnBacklog=[[UIBarButtonItem alloc]initWithTitle:@"Back to log" style:UIBarButtonItemStyleBordered target:self action:@selector(goBacklog)];

    self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:clearHistory,btnBacklog, nil];


- (void)goBack {
    self.tableLogView.hidden = YES;
    self.navigationController.navigationBarHidden=YES;
    self.viewLogToolbar.hidden=NO;
    self.extendedView.hidden = YES;
}

- (void)goBacklog {
    self.tableLogView.hidden = NO;
    self.navigationController.navigationBarHidden=NO;
    self.viewLogToolbar.hidden=YES;
    self.extendedView.hidden = YES;
}

-(void)clearTable
{
    if([tableData count])
    {
    UIAlertView *message= [[UIAlertView alloc] initWithTitle:@"Delete All History" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
        [message show];
    }
    else
    {
        UIAlertView *message= [[UIAlertView alloc] initWithTitle:@"Message" message:@"No History" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [message show];
    }


}

2 个答案:

答案 0 :(得分:0)

UIBarButton没有hidden属性。

仅显示所需按钮的最简单方法是动态更改self.navigationItem.leftBarButtonItemself.navigationItem.rightBarButtonItem值。也就是说,每次更改屏幕时,都会将这些值设置为所需的值。如果您想在更改期间使用动画,请使用

[self.navigationItem setLeftBarButtonItem:actualBarButton animated: YES]

表达式而不是简单的属性赋值。

答案 1 :(得分:0)

像这样修改你的代码:它不会隐藏但是可以实现完全相同的功能。我希望这是你所期待的。

self.navigationController.navigationBarHidden=YES;
   UIBarButtonItem *clearHistory=[[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(clearTable)];
   self.navigationItem.rightBarButtonItem=clearHistory;

   UIBarButtonItem *btnBack=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
   self.navigationItem.leftBarButtonItem=btnBack;



- (void)goBack {
   self.tableLogView.hidden = YES;
   self.navigationController.navigationBarHidden=YES;
   self.viewLogToolbar.hidden=NO;
   self.extendedView.hidden = YES;

}

- (void)goBacklog {
   self.tableLogView.hidden = NO;
   self.navigationController.navigationBarHidden=NO;
   self.viewLogToolbar.hidden=YES;
   self.extendedView.hidden = YES;

   [self.navigationItem.rightBarButtonItem setTitle:@"Clear" ];
   [self.navigationItem.rightBarButtonItem setAction:@selector(clearTable)];
}

将此代码放在函数中,从tableview导航到textview

[self.navigationItem.rightBarButtonItem setTitle:@"Back to log" ];
[self.navigationItem.rightBarButtonItem setAction:@selector(goBacklog)];

如果你仍想在数组中保留2个右键,那么你唯一可以做的就是在两个按钮之间切换启用选项,如

UIBarButtonItem *ubi= [self.navigationItem.rightBarButtonItems objectAtIndex:0];

ubi.enabled=NO/YES;