Trying to handle back navigation button action in iOS
Try this code using VIewWillDisappear
method to detect the press of The back button of NavigationItem:
-(void) viewWillDisappear:(BOOL)animated
{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// Navigation button was pressed. Do some stuff
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
OR There is another way to get Action of the Navigation BAck button.
Create Custom button for UINavigationItem of back button .
For Ex:
In ViewDidLoad :
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
}
-(void)home:(UIBarButtonItem *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Swift :
override func willMoveToParentViewController(parent: UIViewController?)
{
if parent == nil
{
// Back btn Event handler
}
}
How to set delegate on CustomToolbar in rootViewController using a NavigationController setup with initWithNavigationBarClass:toolbarClass:
My guess is that in order to work, the navigationController
needs to be the navigationBar
delegate
. That's why you can not set the navigationBar
delegate
when it is used in a navigationController
.
As you use a subclass of navigationBar
, you can try to have a subprotocol of the UINavigationBarDelegate
in which you add a navigationBarWasSelected method.
In a new UINavigationController
subclass, you implement this method to let the rootViewController
now when the navigation is clicked.
Or, in your custom navigation bar you can send a NSNotification
in the NSNotificationCenter
, and catch it in your RootViewController
.
Draw custom Back button on iPhone Navigation Bar
You need to set up a custom stack of UINavigationItem
objects and push them on to the UINavigationBar. This is the only way I know of to get a true back button. I haven't tested this code, but you should do something like this:
UINavigationItem *previousItem =
[[[UINavigationItem alloc] initWithTitle:@"Back title"] autorelease];
UINavigationItem *currentItem =
[[[UINavigationItem alloc] initWithTitle:@"Main Title"] autorelease];
[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
animated:YES];
To handle when the buttons are pressed you should set yourself as the navigation bar's delegate and implement the UINavigationBarDelegate delegates.
UINavigationController intercepting – popViewControllerAnimated:
Add your refresh code to the viewWillAppear:(BOOL)animated method on the view controller that is about to be displayed. In your case, that is the view controller that's already on the navigation stack.
How to actually catch when back button is pressed?
Try with following UINavigationBarDelegate
.
func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)
UINavigationController: How to cancel the back button event?
viewWillDisappear
is a delegate method for the event that the view is going to disappear - and there's nothing the developer can do about that! If you could, it would be a viewShouldDisappear
delegate method.
So I guess the only way is as you suggest, to use a custom leftBarButtonItem
.
Related Topics
Sirikit, How to Display Response for Start Workout Intent
Ambiguous Use of Registerclass with Swift
How to Use Alamofires Servertrustpolicy.Disableevaluation in Swift 3
Avplayerlayer Shows Black Screen But Sound Is Working
Circular Button Becomes Rounded Rectangle After Size Increase
How to Go Back to Rootviewcontroller from Presentview Controller
How to Set the Local Storage Before a Uiwebview Loading Its Initial Request
Reversing a Range Results in Mismatching Types
How to Get the Index (String.Index) Value from the Cursor Position of a Uitextview Element in Swift
Schedule Local Notification Every N Days (Timezone Safe)
How to Work with Udp Sockets in iOS, Swift