Is there an inverse of `setEditing` for when editing is complete? - iphone

I'm working on a traditional iPhone UINavigationController app, with automatic back buttons etc.
I am working on when an 'edit' button is pressed. The LHS back icon dims, my new one comes in, and then once I tap the 'edit' button again, the back button comes back.
So far, the back button goes away, and my new one comes in, but I can't put it back! I know what the code should be, but I don't know where to call it.
Here is what I have so far:
(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[self.navigationItem setHidesBackButton:editing animated:animated]; //fades back button
//de 006 - Load in Move section button here.
UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self action:#selector(altersection:)] autorelease];
self.navigationItem.leftBarButtonItem = saveButton;
Basically I want the inverse of (void)setEditing:(BOOL)editing animated:(BOOL)animated {, where I can do:
self.navigationItem.leftBarButtonItem = nil; //custom button hide
self.navigationItem.hidesBackButton = NO; //replace back button
Is there an inverse of (void)setEditing:(BOOL)editing ?

Not sure I completely understood the question :/
When you press the "Done" button, I believe setEditing get's called again, but with NO as the editing parameter.
So in setEditing you could check for:
if(editing) { .... }
To see if we are entering or leaving the editing state.

You just have to pu if condition in setEditing. Same method gets called on any action on edit button.
You can have code like
if(self.navigationItem.leftBarButtonItem)
{
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = NO;
}
You can also check for both conditions in if.

Related

Enable Edit Mode in ViewController

I'm new on iOS development, i m trying to show a user profile inside a view, and in the same time i want to give a user a possibility to edit his profile by tapping an "Edit" button on the UINavigationBar like shown on Apple web site : Enabling Edit Mode in a View Controller
I tried to find a tutorial explaining all this, but i didn't fin anything. can somebody help me plz, by giving me a link for a tutorial, or a sample code ?
PS : I m using storyboard.
Thanks a lot !
Here and here are a couple of examples for a UITableview
The concept is the same. You add a UIBarButtonItem and change the current mode of the tableView and the status(text) of the buttonItem to show the editing dashes and other content if you choose.
Here is a simple edit mode button press to send the tableView into edit mode to allow for easy deleting. You can also
- (IBAction)editPressed:(id)sender
{
// If the tableView is editing, change the barButton title to Edit and change the style
if (_theTableView.isEditing) {
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:#"Edit" style:UIBarButtonSystemItemDone target:self action:#selector(editPressed:)];
self.navigationItem.rightBarButtonItem = newButton;
_buttonEdit = newButton;
[_theTableView setEditing:NO animated:YES];
}
// Else change it to Done style
else {
UIBarButtonItem *newButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonSystemItemEdit target:self action:#selector(editPressed:)];
self.navigationItem.rightBarButtonItem = newButton;
_buttonEdit = newButton;
[_theTableView setEditing:YES animated:YES];
}
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
// You could do other things in here based on whether editing is true or not
}
You can set default edit button to you navigationItem barbutton inside viewDidLoad as shown below.
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
As give in the apple document
editButtonItem - Returns a bar button item that toggles its title and associated state between Edit and Done. The default button action invokes the setEditing:animated: method.
Override setEditing:animated: in your view controller as shown below.
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
}
You can make use of bool variable editing to achieve your requirements.

UINavigationItem BackBarButtonItem not replaced

I'm having a curious issue with backBarButtonItem. I want to replace its title in the entire application for "Back" and replacing the back button works in most of -viewDidLoad event but in other views it's not working and show the name of the previous view. Has someone has the same problem?
P.S. The way to replace the backBarButtonItem is the standard one instantiating an UIBarButtonItem and setting it to viewController.navigationIten.backBarButtonItem property.
The backBarButtonItem does not set the back button that is shown in the current view, it sets the back button that navigates to the current view, i.e. the back button in the next view.
This makes sense because the back button's title is usually the title of the previous view controller.
If you want to set the left button in the navigation bar directly, use self.navigationItem.leftBarButtonItem.
when you push the view from your current view at that time after allocate your next viewcontroller object ,just put bellow line
YourViewController *objView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.navigationItem.title=#"Back";
[self.navigationController pushViewController:objView animated:YES];
your Next View will Appear with Back Button....
:)
Well, at last I've found the solution to this issue.
If you want that any backBarButtonItem of your application has the same title a good approach is to subclass UINavigationController and override - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated to replace the back button.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BackButtonLabel", "")
style:UIBarButtonItemStyleDone
target:nil
action:nil];
viewController.navigationItem.backBarButtonItem = _backButton;
_backButton = nil;
[_backButton release];
[super pushViewController:viewController animated:animated];
}
By this way every back button in your application will have the same title.
I hope this will be helpful for anyone else.

How to tell when back button is pressed in a UINavigationControllerStack

Is it possible to check when the back button is pressed in a UINavigationController stack? I've tried adding a action and target to self.navigationItem.backBarButtonItem to no avail.
Anyone have any solutions?
You can try my way:
Write in your ViewController:
UIBarButtonItem *backBt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"imageNameOfBackButton"] style:UIBarButtonItemStyleBordered target:self action:#selector(backBt_touch:)];
self.navigationItem.leftBarButtonItem = backBt;
And action method:
- (void)backBt_touch:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
You have to take a photo of back button you want.
Animation of hiding back button when viewController is popped is not the same animation of iOS!
P/s:
I've get it from simulator. Hope it useful! :)
One way to get at this would be to override viewWillDisappear in the UIViewController that is visible when the back button is pressed:
- (void)viewWillDisappear:(BOOL)animated {
if (self.isMovingFromParentViewController) {
// handle back button press
}
}
Obviously this doesn't directly intercept the press on the back button itself, but it gives you a chance to perform logic at that time.

How do I replace the back button with another UIBarButtonItem in a navigation controller when editing?

I have a standard UINavigationController setup. One particular view displays an "Edit" button on the right side of the Navigation Bar. When that button is pressed and the view switches to editing mode I would like to replace the "Back" button on the left side with an "Add" button. Of course, when editing is finished (the user presses "Done") the button on the left side should change again to the back button.
The obvious answer works. Assuming you have declared a UIBarButtonItem called addButton, you can implement setEditing:animated: as follows:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
self.navigationItem.leftBarButtonItem = addButton;
} else {
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
}
}

Is there a better way to hide the backBarButtonItem than this?

I have a way of hiding the back button used by the navigation controller. It's set by the previous controller, not the one managing the current view, and that makes it tricky to get to. I needed to do this in editing mode so that I could prevent the user from navigating away from the screen.
if(self.editing) {
// Get rid of the back button
UIView *emptyView = [[UIView alloc] init];;
UIBarButtonItem *emptyButton = [[[UIBarButtonItem alloc] initWithCustomView:emptyView] autorelease];
[self.navigationItem setLeftBarButtonItem:emptyButton animated:YES];
} else {
// Restore the back button
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
}
Is there a better way to do this?
use this to hide back button
[self.navigationItem setHidesBackButton:YES]
use this to show back button
[self.navigationItem setHidesBackButton:NO]
Here's the method I use in my view controller to show and hide the back button when editing is enabled and disabled:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
if (editing) {
// Disable the back button
[self.navigationItem setHidesBackButton:YES animated:YES];
}
else {
// Enable the back button
[self.navigationItem setHidesBackButton:NO animated:YES];
}
[super setEditing:editing animated:animated];
}
Make an outlet with strong (not weak as default) of the bar button from the storyboard to your view controller.
The purpose is not to loose the reference when you set the left/right bar button to nil.
Swift5:
self.navigationItem.setHidesBackButton(true, animated: false)