UIBarButtonItem with custom title - iphone

I have the following code:
UIBarButtonItem *promoteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(promoteToInstagram:)];
[promoteButton setTitle:#"Promote"];
self.navigationItem.rightBarButtonItem = promoteButton;
I am trying to create a UIBarButtonItem with a custom title. Why does the above says cancel instead of Promote?
Any idea on how to fix this?

Use the correct initializer, perhaps?
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

UIBarButtonItem btnSort = [[UIBarButtonItem alloc]initWithTitle:#"Descending" style:UIBarButtonItemStyleBordered target:self action:#selector(pressAscending)];
[[self navigationItem] setRightBarButtonItem:btnSort];

Related

How to add UIButtons to navigationbar ,the class also consists UITabbarController

I am new at iOS Development.I Have UINavigationController as rootviewcontroller for UIWindow.In the subclass i added UITabbarController programatically.I given default Tabbarcontroller selection as first controller. Here i am trying to add 3 buttons to the navigationbar .
Can any send me the corrent code.
Thanks In advance
To add more than one button on the left or the right side you have to call one of the following methods:
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated
The array items contains instances of the UIBarButtonItem class. You can instantiate them like this
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonTapped:)];
Then you have to implement the selector buttonTapped: which is called when the button has been tapped.
-(void)buttonTapped:(UIBarButtonItem *)button
{
// do the things that should happen when the button is pressed
}
If you don't want to get them on the left or right side it is also possible to create a UIView by yourself containing the buttons and set the view to be the titleView of the UINavigationItem. This effect is similar to the buttons in the Facebook App
[self.navigationItem setTitleView:yourView];
Here is simple code for add UIButton on UINavigationBar
In Following code you can add Button With FlexibleSpace
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:#selector(FirstTapped:)];
[barItems addObject: btnFirst];
UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:#selector(SecondTapped:)];
[barItems addObject:btnSecond];
UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:#"Third" style:UIBarButtonItemStyleBordered target:self action:#selector(ThirdTapped:)];
[barItems addObject: btnThird];
self.navigationItem.rightBarButtonItems = barItems;
Button Related Methods
-(void) FirstTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) SecondTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) ThirdTapped:(UIBarButtonItem *)sender{
//perform your action
}
NOTE : self.navigationItem.rightBarButtonItems Works only in iOS 5 or Latter.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
// here is custom button setup //
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self.navigationItem setLeftBarButtonItem:item animated:YES];

inbuilt save button text

I want to change here title of button can i change by default it is coming 'save' can it be changed
UIBarButtonItem *dowButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:#selector(download)];
strong text
Create an own UIBarButtonItem instead of using the UIBarButtonSystemItemSave:
UIBarButtonItem *dowButton = [[[UIBarButtonItem alloc] initWithTitle:#"Download" style:UIBarButtonItemStylePlain target:self action:#selector(download)] autorelease];
This will helps you :-)
Try Using the method
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

add a "+" button in a navigationBar

I try to add a "add button" in the navigationBar, but the button who is created is a rectangle button with no text inside, not a "+" button, why?
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonSystemItemAdd target:self action:#selector(ajouterItem)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
You're mixing up the initializer with the one for title/style. You want this form:
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action
Call it like this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(ajouterItem)];

How to disable a UINavigationBarItem?

I'm trying to disable a button that I add to my navigation controller bar. Here is how I added it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
What is the best way to enable/disable items like these? I've tried this code:
addButton.disabled = YES;
but it doesn't work of course. Any help would be appreciated. Thanks.
Edit: Should be addButton.enabled = YES;
Oops
If you define addButton in your header, and #synthesize it, then you will be able to use addButton.enabled = NO;, there is no "disabled" setter.
.h
#interface MyViewController {
UIBarButtonItem *addButton;
}
#property(nonatomic,retain) UIBarButtonItem *addButton;
#end
.m
#implementation MyViewController
#synthesize addButton;
-(void)viewDidLoad{
addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
-(void)DoSomething{
addButton.enabled = NO;
}

how to set custom method with rightnavagation Button in NavBar

I want set my custom method "Home" with RightNavagation button how should I?
My code
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home:)];
[barButton setImage:[UIImage imageNamed:#"home_btn.png"]];
[self.navigationItem setRightBarButtonItem:barButton];
Know I want to link this method with it.
-(IBAction)home{
MainViewController *main=[[MainViewController alloc]
initWithNibName:#"MainViewController" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
}
how should I Do this please help me out.
Try to change
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home:)];
in
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home)];
compare your selector #selector(home:) with your action -(IBAction)home
See the missing colon in your action?
Change your action to
- (IBAction)home:(id)sender