In iOS project I need to check if the back button is currently displayed. I've tried some solutions provided on SO, but none of them worked for me. Currently I'm working with this code
NSArray *stack = navigationController.viewControllers;
int i = stack.count-2;
if (i>=0)
{
UIViewController *backVC = (UIViewController*)[stack objectAtIndex:i];
if (backVC.navigationItem.backBarButtonItem != nil) {
NSLog(@"Back button is displayed!");
}
}
But nothing appears in log. If I understood apple guides correctly, I'm looking for ViewController that sits int the stack at index n-2, that ViewController is supposed to hold a back button.
I'm using this code inside -navigationController:willShowViewController:animated:
This is more reliable and probably the right way to do it
Swift :
guard let navigationStack = navigationController?.viewControllers, navigationStack.count > 1 else {
// The back button is not present
return
}
// The back button is present