0

我创建了一个具有视图、表格视图和文本视图的应用程序,我已经设置了我的应用程序,当用户单击按钮登录视图时,它会将他们带到表格视图并单击表格视图中的单元格将他们带到文本视图。对于所有这些,我只使用了一个 xib。一切正常。现在,当我导航到表格视图时,UIBarButton 有 3 个按钮,返回视图(左),返回表格视图(右)和一个清除按钮(右),当我单击表格视图中的一个单元格时,它会转到文本视图和所有 3 个按钮仍然存在。我需要的是当 tableview 出现时,按钮 Back to log 应该被隐藏,其他 2 应该是可见的,当我在 textview 上时,Back to main 和 Back to log 应该是可见的,并且应该隐藏清除按钮。有没有办法做到这一点?这些是我的 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];
    }


}
4

2 回答 2

0

UIBarButton没有hidden财产。

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

[self.navigationItem setLeftBarButtonItem:actualBarButton animated: YES]

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

于 2013-08-16T05:37:06.043 回答
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;
于 2013-08-16T09:37:11.783 回答