Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top