0

I have created a application which has a view, tableview and a text view, I have setup my application in a way that when a user click on button log in view it takes them to tableview and clicking on a cell in table view takes them to textview. I have used only single xib for all these. Everything works fine. Now when I navigate to table view the UIBarButton has 3 button, Back to view(Left), Back to table view(Right) and a clear button(Right) and when I click on a cell in table view it goes to text view and all 3 button are still there. What I require is when the tableview appears the button Back to log should be hidden and other 2 should be visible and when I am on textview, Back to main and Back to log should be visible and clear button should be hidden. Is there a way to achieve this?? These are my codes for the 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];
    }


}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Francis F
  • 3,157
  • 3
  • 41
  • 79

2 Answers2

0

The UIBarButton doesn't have a hidden property.

The easiest way to show only the desired buttons is to dynamically change the self.navigationItem.leftBarButtonItem and self.navigationItem.rightBarButtonItem values. That is, each time you change screens, you set these values to the desired ones. If you want animations during the change, use the

[self.navigationItem setLeftBarButtonItem:actualBarButton animated: YES]

expression instead of the simple property assignment.

allprog
  • 16,540
  • 9
  • 56
  • 97
0

Modify your code like this: It doesn't hide but achieves the exact same function. I hope this is what u are expecting.

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)];
}

Put this code inside function where u navigate from tableview to textview

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

If you still want to keep 2 right buttons in array then only thing u can do is toggle the enable option between the two buttons like

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

ubi.enabled=NO/YES;
Raon
  • 1,266
  • 3
  • 12
  • 25