how to disable click on back button, iphone - iphone

I am having back button and edit button on my navigation bar like below
After clicking on the Edit button, I have
My question : how can I disable click on back button when I am in edit mode so that user can not go back previous screen until he or she is done.....
What I am trying is
self.navigationItem.backBarButtonItem.enabled = NO;
but the back button is still clickable
PS : The way I add back button to navigation bar is
self.navigationItem.hidesBackButton = NO;
I can hide back button but I dont want that option...
Please advice me on this issue. Any comments are welcomed here.

The best solution would be to end edit mode when you are leaving the screen, maybe displaying a confirmation alert first (UIAlertView with two buttons "Ok" and "Cancel").
However, to answer the question - you would have to create a UIButton with the same appearance as a back button (using images). Create a UIBarButtonItem with this button as its custom view and use it in leftBarButtonItem (note that backBarButtonItem cannot have custom views).
Then you would be able to set enabled to NO on that custom view.
EDIT: I was wrong. UIBarButtonItem has enabled property. The problem with disabling the back button was there probably because you were disabling backBarButtonItem on the wrong navigationItem. The back button is always defined by the previous controller in the stack.

If the button is blocked maybe you should hide it. Because in my point of view having a unresponsive button is not good for the user.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[UIView animateWithDuration:0.35f animations:^{
self.navigationItem.hidesBackButton = editing;
}];
}

self.navigationItem.backBarButtonItem.enabled = NO;
Where you put the above code. place the above code in edit button's IBAction method.

Related

Flipping view causes back button to appear on navigation bar

I have explicitly set the navigation bar's back button to be hidden by the following method -
[[self navigationItem] setHidesBackButton:YES]
This works fine when I enter this view from a view (which is flip-able, it's a map view). The back button does not appear if I enter the view without flipping the previous view (the map view). However, if I enter this view after flipping the map view, a back button appears. And this button does not function at all on tapping it.
I have tried setLeftBarItem:nil method, but even then it appears (and doesn't function)!
Anyone have a solution to this really odd problem?
You need this
self.navigationItem.hidesBackButton = TRUE;
OR
[self.navigationItem setHidesBackButton: YES animated: YES];

Hide BackButton in UINavigation bar

i am pushing a View om RootView controller ,i get the navigation bar with back button which is navigate to rootviewcontoller,but i don't need this back button.i pout this code in the viewcontoller to hide the back button
self.navigationItem.hidesBackButton = YES;
so i get the hidden back button,but here is the problem,i need leftnavigationbar button to show something,when i write the above code ,the letbarbutton also hidden from the view.becz the leftbarbutton act as the back button.
My need is to block the letbarbutton to become a back button when push from the rootviewcontroller,i didn't need a navigation back to rootviewcontoller.But defenitly need leftbarbutton there for my need in the view.
How can it possible to do?.Please help me to do this.
Thanks in advance.
try this.
self.navigationItem.backBarButtonItem = nil;
it works for me
Why don't you just set the letbarbutton and add no action to it?

Hiding ToolBar in a UIViewController

I have a UIViewCOntroller, and in that i have a button and a text field. When i click the button i display a UIToolBar.
Now when i click anything in the background (the textfield or the blank view) i need this UIToolBar to disappear. How can i do this programmatically ?
I know how to add a UIToolBar but all what i need to know is to hide it when the user clicks on the background.
I don't think i will have to paste any code here or show my workings so far, coz i have no clue how to get this done
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:YES animated:YES];
}
May be it can help you....
You will need to capture a touch on the views outside of your toolbar to achieve this. If you have a custom UIView base class that all of your other views use, you might start there. Otherwise, perhaps use some sort of toggle to show/hide your toolbar instead in your UIViewController.
The easiest way to do this is to make a single large clear button that is behind the first button but above everything else. Normally have it set to hidden but when you show the toolbar unhide the button as well. When the button is clicked have it hide the toolbar and its self. No need to do anything crazy like sub classes.

Custom behavior when "back" button in nav bar pressed

I know that you can add a custom "back" button to a UINavigationBar, but doing so removes the existing button with the tapered left side, as described in What happens when back button is pressed in navigationBar
Is there any way to keep the existing behavior and look of the back button, but also be informed when it is pressed?
Basically, I want my app to play a sound whenever any button is touched. I can do it with UITabBarController via its delegate, but the delegate for UINavgationBarController has no such functionality.
The following are the possibilities for a View Controller to disappear.
Clicking on a button (other than backBarButtonItem).
Clicking on the backBarButtonItem
Either case viewWillDisappear: method will be called. Keep a boolean flag. Lets name it isAnyButtonClicked. And set isAnyButtonClicked = YES whenever any button is clicked. And override the viewWillDisappear: method, and play the sound if no button is clicked(ie., isAnyButtonClicked == NO). Probably your viewWillDisappear: would look like,
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (!isAnyButtonClicked) {
// Play sound
isAnyButtonClicked = NO;
}
}

Disable navigationitem backbarbutton

How do I disable the backbutton in a navigationcontroller?
When I hide the button like so,
self.navigationItem.hidesBackButton = TRUE;
the buttonarea is still tappable.
If you don't need a back button and since part of navigationBar's existence is to have a back button you can simply hide the navBar. I can post the code to that if you want as Im doing that in couple of projects. Otherwise have your "bug" might not be a bug if you are calling it from the wrong place.
After digging i managed to come up with this.
To hide the back button use:
self.navigationItem.hidesBackButton = YES;
To hide the whole navigationBar use:
self.navigationController.navigationBarHidden = YES;
But this will happen without animation, to animate it use this instead:
[self.navigationController setNavigationBarHidden:YES animated:YES];
This will make a sliding animation together with the navigation view. You will need to provide some other means for the person to get back tho.