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;
}
}
Related
I want to change between two view controllers using a button, on an iPhone. I don't want to use UITabBarController because it takes too much space, but I want the same kind of segue. How do I do that? Do I need to write a custom segue ?
A Push Segue is probably what you want as its operation can be reversed allowing the button (or stepper) to dictate navigation.
To initiate;
[self performSegueWithIdentifier:#"DestinationViewController" sender:self];
To return either use the back button that will appear in the navigation bar or programmatically with a hidden back button.
[self.navigationController popViewControllerAnimated:YES];
To hide the back button:
self.navigationItem.hidesBackButton = YES;
called in the -(void)viewWillAppear part of the lifecycle.
I am launching a viewController from another view controller via the push on its table views cell. Now on the second view controller I have a whole bunch of controls mainly test fields. I would like to by using the default back button provided in the second view controller so it'll be the title of the first view controller and keep in mind like I said default, so I don't want to create my own button for back on the second view controller. So would like to detect if the second view controller is exiting or disappearing or will disappear and based on certain conditions stop it from going back to the original caller view controller. I originally assumed it could be done in here:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// So back button was pressed on the second view controller, so how do I stop it
// here from going back to the original view controller.
}
}
Or how do I do it? I can't seem to find a view controller return type BOOL method to call and stop it.
Thanks.
Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.
You can't prevent the view controller from closing however you can emulate it.
This line will replace the close button with a button that will call a homemade function
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(closeBtn)];
So you will be able to close the view if you want:
- (void) closeBtn
{
bool shouldClose=true;
// Your conditions here
if (shouldClose) [self dismissViewControllerAnimated:YES completion:nil];
}
sorry,i'm not good at english,so reading so long text is a little difficult for me.i can only get that you want to go back without a button. i known navigationController has a method to go back to previous view.
Make a custom back button and use self.navigationItem.hidesBackButton=YES
Add target to your custom back button and use it as you like. When you want to move the view you can use [self.navigationController popViewcontroller].
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.
I currently have a UIViewController "Radio" with a button on it.
When the button is clicked, the "Radio" viewcontroller pushes another UIViewController called "details"
When the user hits "Back" and returns back to the "Radio" view controller, I need a method to detect that it did so, to prevent refreshing the View.
Does anyone know how I can do this?
In the Radio class:
- (void) viewDidAppear:(BOOL)animated {
NSLog(#"viewDidAppear");
}
Is there a way to connect code to a view's 'back' button that is part of a navigation controller? That back button is automatically coded in by the nature of it being a navigation controller so I am not sure how I can connect code to it.
(I know this should be really easy but I can't seem to find it in the documentation on navigation controller.)
viewWillDisappear is where you would typically add code to execute when the back button is pressed
In reality you should not have a code that needs to be executed when Back button is pressed. What exactly you're trying to achieve?
You could check if the controller was popped in viewWillDisapper, like in this example:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(#"Article done");
}
}