0

In my application I am added two button at right of a UINavigationItem, its working fine on simulator, but when I testing it on device its gives me error of SIGABRT, along with unrecognized selector sent to NSArray. I tried to add one button at right side, it was added successfully, and works fine on device as well. Here my question is, whats the problem?

I am adding right buttons using following code,

NSArray *buttons=[[NSArray alloc] initWithObjects:btnOne,btnTwo,nil]];
myNavItem.rightBarButtonItems=buttons; //Error on device, but works fine on simulator.

Please, point me what is I doing wrong? Thanks!

Hemang
  • 26,840
  • 19
  • 119
  • 186

4 Answers4

0

try adding these buttons to a UIBarButtonItem and add UIBarButtonItem to myNavItem like myNavItem.rightBarButtonItem = barButtonItem;

Anu
  • 16
  • 1
  • Anu, rightBarButtonItem is working fine, but rightBarButtonItems has problems. I added buttons of type UIBarButtonItem only. – Hemang Apr 11 '12 at 04:54
0

Your first line

NSArray *buttons=[[NSArray alloc] initWithObjects:btnOne,btnTwo,nil]];

has an extra right bracket at the end. Not sure if this would cause that error but it should cause some error.

ScottF
  • 565
  • 6
  • 21
0

You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:btn1,btn2,nil]];
   [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
   segmentedControl.frame = CGRectMake(0, 0, 90, 35);                                                 

 segmentedControl.segmentedControlStyle=UISegmentedControlStyleBar;                            
 segmentedControl.momentary = YES;       
 UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];        
 [segmentedControl release];       
 self.navigationItem.rightBarButtonItem = segmentBarItem;  
     [segmentBarItem release]; 
 } 

This is the best way of adding as many number of buttons in your bar as you desire.Hope it gonna help u. Thanks :)

Nikhil Bansal
  • 1,545
  • 13
  • 29
0

It appears that myNavItem is not an instance of UINavigationItem, but rather an instance of NSArray (which does not support setRightBarButtonItems). Could you show us more lines concerning myNavItem?

My suspicion is that myNavItem did not correctly retain the navigation item that it was originally pointing to. And that it points to an NSArray now by coincidence. This error might not occur in a debug setting if all objects are retained indefinitely for better logging.

If this code runs from an instance of a view controller try to use this line instead:

self.navigationItem.rightBarButtonItems = buttons;

On iOS prior to version 5: if you receive unrecognized selector sent to NSArray logs there is something wrong with your memory management. The log should read unrecognized selector sent to UINavigationItem on iOS prior to iOS 5.

Once the memory issue is fixed you should use a UIBarButtonItem with a custom view containing two UIButtons.

Felix Lamouroux
  • 7,414
  • 29
  • 46
  • FelixLam, thanks for the reply! I read your comment that `Beginning with iOS 5 it is possible to add several bar buttons to the navigation items`. I was tested my app on iOS 4 – Hemang Apr 11 '12 at 09:27
  • FelixLam, thats right!. I tried with custom view, and add it to subview of `UIBarButtonItem`, but I've some programming issues so I need to use the buttons those like `UIBarButtonItems` and not `UIButton` – Hemang Apr 12 '12 at 04:38
  • If you want to support iOS 4 there is no other clean way of doing it than using a `UIView` with multiple `UIButtons` as subviews and to create a `UIBarButtonItem` with that `UIView` as custom view. You might to create some PNGs as background images for those buttons which may look like default bar button items. Look here for a tutorial: http://osmorphis.blogspot.de/2009/05/multiple-buttons-on-navigation-bar.html – Felix Lamouroux Apr 12 '12 at 11:02
  • In any case I believe that the answer above answered your immediate problem about the strange behavior. The question you are asking now is new (but well covered elsewhere on SO): How to add multiple buttons to a UINavigationItems on iOS 4? – Felix Lamouroux Apr 12 '12 at 11:03
  • FelixLam, thats the ultimate solutions! I should try for it. Thanks for continuous help:) – Hemang Apr 12 '12 at 12:01