Adding buttons to navigation bar - iphone

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

Related

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

How to set action in multiple buttons in navigation bar

this is my code
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#"button1", #""),
NSLocalizedString(#"button2", #""),
NSLocalizedString(#"button3", #""), nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] nitWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
three buttons are displayed but i don't know how to set action in button help me thanks in advance
add target at the time of segmentedControl creation like below
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
in that selector
-(IBAction) segmentAction:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.segmentLabel.text =#"Segment 1 selected.";
break;
case 1:
self.segmentLabel.text =#"Segment 2 selected.";
break;
default:
break;
}
You have to make 3 buttons and add them to an UIToolbar.
- (void)viewDidLoad
{
[super viewDidLoad];
// create a toolbar to have the buttons at the right side of the navigationBar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 44.01)];
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *button1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:button1];
[button1 release];
// Create button2
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:button2];
[button2 release];
// Create button3
UIBarButtonItem *button3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(button3Pressed)];
[buttons addObject:button3];
[button3 release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
...
- (void)button1Pressed
{
//do stuff
}
- (void)button2Pressed
{
//do stuff
}
- (void)button3Pressed
{
//do stuff
}

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

How can I add two UIBarButtonItems to UINavigationItem?

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

Disabling UINavigationItem custom right view?

I create a custom right view as follows:
// Build the Segmented Control
NSArray *segmentTextContent = [NSArray arrayWithObjects:[UIImage imageNamed:#"arrow-dice.png"], [UIImage imageNamed:#"arrow-up.png"], [UIImage imageNamed:#"arrow-down.png"], nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
// Customize the Segmented Control
segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
Then I add it to my navigation bar as follows:
// Add the control to the navigation bar right item
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentItem;
self.navigationItem.rightBarButtonItem.title = #"";
[segmentItem release];
I can hide it as follows:
self.navigationItem.rightBarButtonItem.customView.hidden = NO;
QUESTION
...but how can I disable all (or better, a specific element) of the segmented control?
The following does not work.
self.navigationItem.rightBarButtonItem.enabled = NO;
Any ideas appreciated...
Thanks,
matt
UISegmentedControl.h
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeAllSegments;
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; //default is YES
e.g. [segmentedControl setEnabled:NO forSegmentAtIndex:1];
Hope That helps
[navItem.rightBarButtonItem setEnabled:NO];
as simple as that :)
Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html
See the enabled property.