Navigation Controller back button - ios

How can I create a back button of a navigation controller programmatically?

In -(void)loadView or similar:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = btn;
[btn release];
-(void)backPressed: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}

Related

set the back button from the pushViewController view

I have a viewController A, is it possible that I pre-set the back button of viewController B when it is pushed to.
Controller *B = [Controller viewControllerFromStoryboardWithProfile: profile editMode: NO];
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Home"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[B.navigationItem setBackBarButtonItem:newBackButton];
[self.navigationController pushViewController:B animated:YES];
Something like this?
Try this code:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Home"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:anotherViewController];
source: How to create backBarButtomItem with custom view for a UINavigationController

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;

How do I add a right button to a UINavigationController that pushes another view controller?

I have a view controller connected to a navigation controller. I want to add a right button to the navigation bar on the top that will push another view controller. If I add the right button with:
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:#"Map" style:UIBarButtonItemStylePlain target:self action:[self forward]];
self.navigationItem.rightBarButtonItem = Button;
the button appears. However, if I add the following lines to the forward method, the button disappears again:
MapViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:#"map"];
[self.navigationController pushViewController:viewController animated:YES];
What is the correct way of doing this?
there is a problem with the line action:[self forward]] while declaring UIBarButtonItem in your code...change it to #selector(forward) or try this
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(showNextVC)];
self.navigationItem.rightBarButtonItem = nextButton;
and in showNextVC:
-(void) showNextVC {
MapViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"map"];
[self.navigationController pushViewController:viewController animated:YES];
}
Try this code
UIImage *rightBtnImage = [UIImage imageNamed:#"bulb-plus.png"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:rightBtnImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, 26, 35);
[rightButton addTarget:self
action:#selector(rightBarButtonAction)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
IBAction method for rightBarbutton
- (void)rightBarButtonAction {
[self performSegueWithIdentifier:#"IdeaDetailSegue" sender:self];
}
Hope this helps.

Add Back Button back into navigationController after adding custom button

I'm trying to implement a navigationcontroller that has a uitableview in it. This tableview should be editable, and to add rows to the table the user should press a plus button on the top bar on the screen. However, when I add a uibarbuttonitem and set it to the left side, the back button doesn't re-appear? how would i make it reappear?
The edit button:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButtonPressed)];
[[self navigationItem] setRightBarButtonItem:button];
The editButtonPressed method:
[_actionList setEditing:TRUE];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAction)];
[[self navigationItem] setLeftBarButtonItem:addButton animated:TRUE];
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editButtonDone)];
[[self navigationItem] setRightBarButtonItem:editButton animated:TRUE];
[self disableButtons];
The editButtonDone method:
[_actionList setEditing:FALSE];
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButtonPressed)];
[[self navigationItem] setRightBarButtonItem:editButton];
[self enableButtons];
I'm a beginner so sorry if this is dumb.
Try resetting left bar button when you leave edit mode.
[[self navigationItem] setLeftBarButtonItem:nil animated:YES];
try this:
[yourNavBar pushNavigationItem:self.navigationItem animated:NO];
Hope it helps

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