How to set action in multiple buttons in navigation bar - iphone

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
}

Related

How to add title and Done button on toolbar in a modal view

I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}

UIBarButtonItem action not working.Why?

Thanks to all. I found really working code. It looks like:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(settingsClick) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* theSettingsButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[self.toolbar setItems:[NSArray arrayWithObjects:theSettingsButton,nil]];
[theSettingsButton release];
}
This works:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)] autorelease];
}
-(void)settingsClick
{
NSLog(#"Hello");
}
You hadn't closed your viewDidLoad method.
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:theSettingsButton, nil];
[self.toolbar setToolbarItems:arr animated:YES];
try this
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSArray * arr = [NSArray arrayWithObjects:theSettingsButton, nil];
[toolbar setItems:arr animated:YES];
[self.view addSubview:toolbar];
[toolbar release];

Segment Button is not displaying on the navigation bar in iphone

Hello
I am having problem in displaying segmented button on the navigation bar
I try this code it's not working
I have the Tab Bar For calling this tabBar I use
This code TabBar is working fine but Button on the navigation bar is not displaying
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITabBarController *tabcontroller = [[UITabBarController alloc] init];
UITabBarController *vc1 = [[TMapViewController alloc] init];
UITabBarController *vc2 = [[TShareController alloc]init];
UITabBarController *vc3 = [[TSpeedometerController alloc]init];
UITabBarController *vc4 = [[TReviewsController alloc]init];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,nil];
[tabcontroller setViewControllers:viewControllers];
[vc1 release];
[vc2 release];
[vc3 release];
[vc4 release];
[self.navigationController pushViewController: tabcontroller animated: YES];
}
And right this code into the TMapViewController.m for calling
The button onto the navigation bar
-(id)init
{
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
return self;
}
Same thing i use into the
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
[[self.navigationBar topItem] setTitleView:segmentedControl];
[[[self.navigationBar topItem] titleView] setUserInteractionEnabled:YES];
TLocationManager *manager = [[TLocationManager alloc] init];
self.locationManager = manager;
self.locationManager.delegate = self;
[manager release];
TMapRoutes *routes = [[TMapRoutes alloc] init];
self.mapRoutes = routes;
self.mapRoutes.mapView = self.myMapView;
[routes release];
self.myMapView.delegate = self;
/* Run the simulator */
[locationManager startUpdatingLocation];
}
even it not work it give me same error .
Thanks in advance
Harish
Set it as [self.navigationBar titleView:segmentedControl]
-(void)loadView {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
[[self.navigationBar topItem] setTitleView:segmentedControl]; [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES];
[segmentBarItem release];

Multiple UIBarButtonItems in UINavigationBar

How to Create multiple bar button in navigation bar?
From iOS 5 onwards, you can now do it using setLeftBarButtonItems:animated: or setRightBarButtonItems:animated:
You must use UIToolbar and set the toolbar with buttons:
// create a toolbar where we can place some buttons
UIToolbar *toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
you have to create a view with as much button you required and have to add them on navigation button like following :
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
[infoButton1 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton1 setTitle:#"Back" forState:UIControlStateNormal];
infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton1.titleLabel.textColor = [UIColor whiteColor];
[infoButton1 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton1];
[infoButton1 release];
UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
[infoButton2 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton2 setTitle:#"Back" forState:UIControlStateNormal];
infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton2.titleLabel.textColor = [UIColor whiteColor];
[infoButton2 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton2];
[infoButton2 release];
UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
self.navigationItem.leftBarButtonItem = customBarButtomItem1;
[customBarButtomItem1 release];`enter code here`
I know this question was already closed, but I find that the UIToolbar solution doesn't match visually.
If you instead use a second UINavigationBar set with a UINavigationItem that has a title of nil and the desired buttons you can add more buttons and have a bar that visually matches the original.
For iOS7 and higher, this is the right way to do it. No need for UIToolbar silliness.
- (void)viewDidLoad {
[super viewDidLoad];
[self configureView];
// create three funky nav bar buttons
UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:#"One" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:#"Two" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:#"Three" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
// create a spacer
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
space.width = 30;
NSArray *buttons = #[one, space, two, space, three];
self.navigationItem.rightBarButtonItems = buttons;
}
I hate putting links as answers on SO as they can die anytime so i added relevant code taken from HERE
- (void)viewWillAppear
{
// get a view and :
[self.navigationController.navigationBar addSubView:yourView];
}

UINavigationBar BarButtonItem with Plain Style

i got the following code:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"star.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
IBOutlet UIButton * _myButton;
Then set it as your right bar button item using a custom view, which will eliminate the border:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
This works because UIButton is a subclass of UIView.
Try this instead:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[button setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
Hope it helps.
Or do this in code without IB:
// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:#"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png):
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.
sbwoodside has a solution.
Why not try UI7Kit? Works well for me, easy to use.
UIImage *img = [UIImage imageNamed:#"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:#selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
Try this,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(didPressButton)];