adding a leftBarButtonItem crashes - iphone

So I am trying to set my leftBarButtonItem to my navigation item via the following code:
UIBarButtonItem * leftSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
leftSpacer.width = 10;
self.navigationItem.leftBarButtonItem = leftSpacer;
However, it gives me a program received SIGABRT. Why is this?

use some of this code that may helps you.
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
// stick the buttons in the toolbar
[tb setItems:buttons animated:NO];
// and put the toolbar in the nav bar
self.navigationItem.LeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tb];

Your code sample shows you alloc and init the bar button item, but you show no release. Do you release it? [leftSpacer release]

Related

Align buttons or space between buttons in NavigationBar programmatically

I have created navigation controller via storyboard interface and I have added 4 buttons programmatically on Navigationbar but I don't know how should I justify it would you please help me!
here is the picture:
here is my code for buttons I know That I used rightBarButtonItems but I don't know what should I write instead!
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *menuButton= [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleDone
target:self action:#selector(menu:)];
UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:#"Year" style:UIBarButtonItemStyleDone
target:self action:#selector(year:)];
UIBarButtonItem *weekButton= [[UIBarButtonItem alloc] initWithTitle:#"Week" style:UIBarButtonItemStyleDone
target:self action:#selector(week:)];
UIBarButtonItem *reportButton= [[UIBarButtonItem alloc] initWithTitle:#"Report" style:UIBarButtonItemStyleDone
target:self action:#selector(report:)];
NSArray *buttons = [NSArray arrayWithObjects:menuButton,yearButton,weekButton,reportButton,nil];
self.navigationItem.rightBarButtonItems = buttons;
}
Add flexible spacers between each of the buttons:
UIBarButtonItem *flexibleSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

uitoolbar in navigation bar.

this is a simple problem, but i'm new to xcode dev. I followed a guide online to have multiple buttons on a navigation bar. the edit button on the navgivation bar has a IBAction method called "editButton". which has a (id)sender as parameter. do I get the sender and change the text from edit to done, done to edit?
"UIBarButtonitem *bbi = (UIBarButonItem *) sender;" doesn't seem to be working. how do i get the button in the toolbar in navigationbar?
Thank you.
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "EDIT" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButton:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
-(IBAction)editButton:(id) sender{
UIBarButtonitem *bbi = (UIBarButonItem *) sender;
if (bbi title isequalsString:#"Done){
[bbi setTitle:#"Edit"];
}
}else{
[bbi setTitle:#"Done"];
}
}
The trouble is that you have used a UIBarButtonSystemItemEdit, not a standard uibarbutton.
Try creating it with:
bi = [UIBarButtonItem alloc] initWithTitle:#"Edit" style: UIBarButtonItemStyleBordered target:self action:#selector(editButton:)];
Then use the rest of the code as is.
UIBarButtonSystemItemEdit is a special bar button item that takes care of the "Edit / Done" states for you. No need to manually change the button's text.

How to create space between buttons?

I am already creating multiple buttons but I don't know how to align the buttons.
My code is here:
- (void)viewDidLoad
{
//self.title=#"Asset Management";
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"User Information"];
[listOfItems addObject:#"Regional Settings"];
[listOfItems addObject:#"Configuration"];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGs
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 *propertiesButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:propertiesButton];
[propertiesButton release];
// Create button2
UIBarButtonItem *commentaryButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:commentaryButton];
[commentaryButton release];
// Create button3
UIBarButtonItem *versionsButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(button3Pressed)];
[buttons addObject:versionsButton];
[versionsButton 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];
}
How do I create space b/w the buttons. Pls help me.
Thanks in advance!
You can add spaces between tool bar items using either of the two built-in space button types
UIBarButtonSystemItemFixedSpace and UIBarButtonSystemItemFlexibleSpace.
Fixed Space button
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[fixedSpace setWidth:20];
Flexible Space button
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
Add the space bar buttons in between the other tool bar items.

how to add a button beside left bar controller or right bar controller

how can i add buttons just beside a left barButton or just beside rightbarButton,
i mean i need to have more than 2 buttons on the title bar is that possible?
thanx in advance
You can use a view with two buttons in your leftBarButton -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
As drawn from this blog post:
Think of a toolbar as your container,
instead of the UIView object. And
since a UIToolBar is UIView based, it
can be added to the right side of a
nav bar using the above trick. The
following code shows how you can add
two standard buttons to the right
side:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
Also check the SO post
Adding buttons to navigation bar
I would suggest to use UISegmentedControl to hold all your buttons and use
[[UIButton alloc] initWithCustomView:] to show the segment control.
Here is a sample of how you could go about doing this. I am using a MasterDetailView template that comes with the top navigation bar for this example:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = #"Left button";
rightButton.title = #"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
You can do something like this, there's something called UINavigationItem.rightBarButtonItems
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"] style:UIBarButtonItemStyleDone target:self action:#selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"]
style:UIBarButtonItemStyleDone target:self
action:#selector(something)];
self.navigationItem.rightBarButtonItems = #[settingsButton, menuButton];
i think you need something like this
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=#[firstButton, secondButton];
it will add two bar button items on left of navigation bar. you can make it on right side the same way
self.navigationController.navigationItem.rightBarButtonItems=#[firstButton, secondButton];

Set the position of a UIBarButtonItem Programmatically

How would I be able to set the position of a UIBarButtonItem? Like, I would like to set it to be on either the very right of a UIToolbar or the very left depending on a state.
Thank you.
You do not set directly the position of a UIBarButtonItem in an UIToolbar. Instead you defined the items' order and put flexible space on the left or on the right.
What you can do is:
Create the UIBarButtonItem you want to place (button 1).
Create an UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
If you want to put the button on the left, create an array with (button 1) and (button 2) and pass it to the UIToolbar by using the setItems:animated: method.
If you want to put the button on the right, create an array with (button 2) and (button 1) and pass it to the UIToolbar by using the setItems:animated: method.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Link explains Bar Button Item Methods and Arguments
https://developer.apple.com/library..... Use Fake Item to get exact pinch location on Button...