How can I add two UIBarButtonItems to UINavigationItem? - iphone

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?

You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.
This is what is used in the Mail application to go to next/previous message.
Update
In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):
- (void)viewDidLoad
{
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}

Related

Multiple Barbutton in Navigation Bar not showing on iOS6

I have added array of bar button to the navigation items using the property rightBarButtonItems,it work good for iOS5,when i tested in iOS6 only one bar button item is visible.
UIBarButtonItem *updateButton = [[UIBarButtonItem alloc]
initWithTitle:#"Update"
style:UIBarButtonItemStylePlain
target:self
action:#selector(updateData)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refresh)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:updateButton,refreshButton, nil];
self.navigationItem.rightBarButtonItems=arrBtns;
Is there any new property for iOS6 to add the array of bar button to navigationitem.
Any help would be appreciated,Thanks a lot.
Please use the segmentController on the rightBarButtonItems if you want to add multiButton on rightBarButtonItems of NavigationBar
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"Add",#"Delete",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 80, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setWidth:35.0 forSegmentAtIndex:0];
[segmentedControl setWidth:45.0 forSegmentAtIndex:1];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
Secondly add the second button on other side of the first bar Button.

How to add an image & a system bar button item to a UISegmentedControl?

I have an UISegmentedControl as the navigation bar's right bar button item. This is achieved by the following code...
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Segment1",#"Segment2",nil]];
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Where segmentClicked is the method for segment click action
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = rightBaritem;
[rightBaritem release];
The above code fine and it will show a segmented Control with two segments "Segment1" & "Segment2".
But I want to show an image instead of Segment1 & a system bar button (Say UIBarButtonSystemItemAdd) instead of Segment2.
Image can be inserted in Segmented control by the code,
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:#"<image_name.image_type>"],???????,nil]];
But i dont know how to include UIBarButtonSystemItemAdd in the place of ???????.
Thanks in Advance..
This code may solve your problem.This solved my problem that I was facing at the same time.But don't think this is the accurate one.I got the desired which I need to display.This works fine with two buttons.
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Done:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Krishna:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];

Put self.editbuttonitem into segmentedcontrol as barbuttonitem?

I want to make a NavBar similar to the one in sample 3 of the NavBar sample code, except I want to use the self.editbuttonItem as one of the two buttons in the SegmentedControl. (The other will be a custom add button.) Basically - the end result will be a leftBarButtonItem that's just one button, bringing up a modal view, and a rightBarButtonItem that's a segmented control with both edit and add buttons.
Thing is, it looks like setting up the SegmentedControl needs an array of Strings or Images, but not BarButtonItems. Is there a workaround?
This is the relevant bit from Apple's sample:
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
Instead of the images, I want to put BarButtonItems...
If self.editbuttonitem is a UIBarButtonItem with title "Edit", I think you can do with the following code
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Edit", #"AnotherButtonName"
nil]];
edit based on Charles Bandes's comment
Add an action to the segmentedControl, like the Apple's sample:
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//...
then in segmentAction:, do
- (void)segmentAction:(UISegmentedControl*)sender
{
//if the "edit" item in segmentedControl is selected
if (sender.selectedSegmentIndex == 0)
{
//I assume self is a UITableView instance
//start editing
[self setEditing:YES animated:YES];
}
}
I wrote those code on my PC. However it should work.
You may take a look at [UITableView setEditing:animated:]

Setting segmented control at the centre of the tab

I am working with the segmented control. I am using the following code to set it at the left of the tab.
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle =
UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(100,10,220,35);
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0
animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1
animated:YES];
segmentedControl.selectedSegmentIndex = 1;
[segmentedControl setMomentary:NO];
[segmentedControl addTarget:self action:#selector(segmentSwitch:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
My question is how can we set it at the center of the tab bar, as it is not taking the CGRect() values to set the position.
I think you are trying to add the UISegmentedControl to UINavigationBar not UITabBar. If you want it at the center you can set it as the titleView of navigationItem.
self.navigationItem.titleView = segmentedControl;

Adding buttons to navigation bar

Is it possible to add buttons to the navigation bar using the IPhone SDK?
I already have 2 buttons in the navigation bar as leftBarButton and rightBarButton. I need 2 more buttons. How to implement that?
Its not obligatory that i need them to be included in the navigation bar itself. But since the application contains only a table, i don't think it can be given elsewhere.
You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.
Here is some sample code:
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
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];
}
- (void)segmentAction:(id)sender{
if([sender selectedSegmentIndex] == 0){
//do something with segment 1
NSLog(#"Segment 1 preesed");
}else{
//do something with segment 2
NSLog(#"Segment 2 preesed");
}
}