action of UIBarbuttonItem not called ios - ios

I am trying to call the UIBarButtonItem when the back button of that controller is pressed.This is my code.I have put the initialization in viewDidLoad and viewWillAppear but the method is not being called.
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:self action:(#selector(backToViewController:))];
self.navigationItem.backBarButtonItem = exampleButton;
-(void)backToViewController:(UIBarButtonItem*)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

IT will not add backBarButtonItem with your line as :
self.navigationItem.backBarButtonItem = exampleButton;
You have to Provide leftbarbutton as
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:self action:(#selector(backToViewController:))];
// As you are going with " ", there is no text, that means you have to click at left side randomly, to get affect.
self.navigationItem.leftBarButtonItem = exampleButton;
Update 1
You can provide Custom button with Image created by you, as you want,
UIButton *barCutomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[barCutomBtn setImage:[UIImage imageNamed:#"backImage.png"] forState:UIControlStateNormal];
[barCutomBtn addTarget:self action:#selector(backToViewController:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithCustomView:barCutomBtn];
self.navigationItem.leftBarButtonItem = exampleButton;

Related

How can I add a button to a navigation bar programmatically in xcode

I'm trying to add button to navigation bar i want to add the button without any action any help please?
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc]
initWithTitle:#"Your Button"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(methodName:)];
self.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
And then method
-(IBAction)methodName {
// your code here
}
You should add UIBarButtonItem to Nav bar instead of UIButton. T do that you can call this:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.rightBarButtonItem = button;
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:#"Custom" style:UIBarButtonItemStylePlain target:self action:#selector(customBtnPressed)];
[self.navigationItem setRightBarButtonItem:customBtn];
///// called event
-(IBAction)customBtnPressed:(id)sender
{
//Your code here
}
if UIButton is you really want, try this.
UIButton *btnSample = [[UIButton alloc] initWithFrame:btnFrame];
btnSample.backgroundColor = [UIColor blueColor];
UIBarButtonItem *barBtn_cart = [[UIBarButtonItem alloc] initWithCustomView:btnSample];
self.navigationItem.rightBarButtonItem = btnSample;

Any idea why the custom nav bar back button image isn't displaying with this code?

Any idea why the custom nav bar back button image isn't displaying with this code?
instead of showing the uiimage it's showing the "< Back" default iOS button in text only
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"freccia.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:#selector(backBtnPress)];
could it be this parameter? UIBarButtonItemStylePlain
thanks
use this code:
UIImage *imageBack = [UIImage imageNamed:#"yourImage.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.bounds = CGRectMake(0, 0, imageBack.size.width, imageBack.size.height);
[btnBack setBackgroundImage:imageBack forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = barButtonBack;
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
You usually set the back button in the parent view controller. However, it might just be easier to use leftBarButtonItem instead of backBarButtonItem.
you can solve this problem by two ways
1. use leftBarButtonItem instead of backBarButtonItem
2. try the below code
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];

Can't change back bar button text

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
Thanks.
Try with This code
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"NewTitle"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)
Try with this code:
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"customBack" forState:UIControlStateNormal];
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItem = aLeftBarButtonItem;
For iOS 7 use below piece of code:
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:#"customBack" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:#"backArrow"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backArrow];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8.0;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItems = #[aNegativeSpacer, aLeftBarButtonItem];

Grabbed the action of a UIBarButtonItem

I have code like this in my navigation bar:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(self)];
self.navigationItem.rightBarButtonItem = myButton;
What's the next step to grab when this is actually pressed?
You mad a mistake here by setting #selector(self).
You need to put here the actual function you need to fire:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(doSomeThing)];
And of course:
- (void) doSomeThing{
}
Do like this
UIBarButtonItem *Button=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(myAction:)];
self.navigationItem.leftBarButtonItem=Button;
- (void)myAction:(id)sender
{
//do ur actions
}

How to reload Navigation bar

In UITableView I configure self.navigationItem.leftBarButtonItem with a button. Now I want to reload this button to update text on this button, but it's on Navigation bar. I can update UITableView by
[self.tableView reloadData]
but How to update Navigation bar?
Update: I'm NOT init the title of button, but reload it. So any init code (such as self.navigationItem.leftBarButtonItem.title = #"balabala") doesn't work, because it does NOT take effect immediately.
Just change the text on the leftBarButtonItem?
self.navigationItem.leftBarButtonItem.title = #"Back"; // or whatever text you want
Try this
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;
Hope this helps you..
EDIT :-
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: #"Title" style: UIBarButtonItemStyleBordered target:self action:#selector(OnClick:)];
[[self navigationItem] setBackBarButtonItem: btnBack];
Did you try setting the text, using
self.navigationItem.leftBarButtonItem.title
You need to use the following statement:
self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
In other words you need the setLeftBarButtonItem:animated message instead of the standard one. This will reload the button text with animation. Same applies for the back button if that is what you are interested in changing at some point.
#interface Myviewcontroller : UIViewController
{
UIBarButtonItem *barBtnBack;
UIButton *btnQuestion;
}
//ViewdidLoad
btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[btnQuestion setBackgroundImage:[UIImage imageNamed:#"question-mark2x"] forState:UIControlStateNormal];
[btnQuestion addTarget:self action:#selector(goBacktomain)
forControlEvents:UIControlEventTouchUpInside];
barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
[self.navigationItem setLeftBarButtonItem:barBtnBack];
note- change btnQuestion title when you want to change navigationnar button title
If you want to change the NavigationBar title, try 1 of these:
[self.navigationController.navigationItem setTitle:#"Title"];
[self.navigationItem setTitle:#"Title"];
[self setTitle:#"Title"];
If you wanna change button titles, put this in the same class:
- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setLeftBarButtonItem:leftButton];
}
- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setRightBarButtonItem:rightButton];
}
Examples to set button title and actions:
// Change button titles and actions any time
[self setLeftBarButtonTitle:#"Back" action:#selector(goBack)];
[self setRightBarButtonTitle:#"Something" action:#selector(doSomething:)];
// Change button titles with no actions
[self setLeftBarButtonTitle:#"Nothing" action:nil];
[self setRightBarButtonTitle:#"Nothing" action:nil];
// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];
Try this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"your_title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
I solved it in Swift, but if the keyboard is shown, it hides.
navigationController?.loadView()

Resources