Add Back Button back into navigationController after adding custom button - ios

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

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 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()

Bar button not receiving touch

I have 2 bar buttons on the tool bar , one is not working and the other is working. Some times when we touch the tip of the first bar button it will work, I think this has to be a problem with flexible space between them.
I couldn't find any possible solution, any help will be appreciated!
I'm pasting the code below
editbutton is the first button and mybutton is the second button
-(void) rightButtonToolbar:(UIBarButtonItem*)menuButton{
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,TOOLBAR_WIDTH ,44)];
NSMutableArray *barButtons = [[NSMutableArray alloc]init];
UIBarButtonItem *flexiSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barButtons addObject:flexiSpace];
if (showEditButton)
{
NSLog(#"showeditbutton");
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editOffer)];
[barButtons addObject:editButton];
[editButton release];
}
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:#"Kopiren" style:UIBarButtonSystemItemAction target:self action:#selector(copyOffer)];
[barButtons addObject:myButton2];
if (menuButton != nil) {
menuButtonShown=YES;
[barButtons addObject:menuButton];
}
else {
menuButtonShown=NO;
}
[toolbar setItems:barButtons animated:YES];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithCustomView:toolbar];
[self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];
[barButtons release];
[flexiSpace release];
[rightBarButton release];
[toolbar release];
[myButton2 release];
}

Navigation Controller back button

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.
}

How to add buttons to the top bar of a detail view from a UITableView

When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.

Resources