0

I am trying to add BarButton in UIToolBar of NavigationController. I found on below code on stack over flow. But it is not working in ios7. If anything changed why it shows nothing on tool bar.

 [self.navigationController setToolbarHidden:NO];


 UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Select All"
                                            style: UIBarButtonItemStyleBordered
                                           target: self
                                           action: @selector(selectAll:) ];
  UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:@"Next"     style:UIBarButtonItemStyleBordered target:self action:@selector(goNext:)];
   self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
Gagan Joshi
  • 3,347
  • 2
  • 21
  • 32
  • You need to add it to the view. [self.view addSubview:self.toolbarItems] – Sophy Swicz Aug 13 '14 at 16:59
  • No need to add this to view. As this is default property of Navigation Controller. We only need to SetToolbarHidden Property to NO. My code is working now. By removing a function in my view controller. that overwrite self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ]; – Gagan Joshi Aug 14 '14 at 08:27
  • The above code is working in ios7. it was my mistake. No working fine. – Gagan Joshi Aug 14 '14 at 08:28

2 Answers2

0
//allocate the tool bar 
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];

//  create bar btns
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(Back)];

//set spacing
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(Forward)];

// add btns to the bar
[toolBar setItems:[NSMutableArray arrayWithObjects:back,flexibleSpace,forward, nil]];

// adds the toobar to the view
[self.view addSubview:toolBar];
modusCell
  • 13,151
  • 9
  • 53
  • 80
Feroz
  • 173
  • 1
  • 10
0

I found the problem. Above code is correct. The reason why there is no button showing in the tool bar . i also have this function in my class. this is a empty function. so it overwrite the default self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ]; After Removing the below code. Its working Perfect Now.

 - (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
 { 

 }
Gagan Joshi
  • 3,347
  • 2
  • 21
  • 32