Sunday, December 6, 2015

HOW TO ADD CUSTOM CODE TO NAVIGATION CONTROLLER BACK BUTTON – XCODE IOS

To make navigation bar “Back” button to work according to your need. Use one of the techniques below –
1. Replace the “Back” button with a new button. The only problem with this technique is that the new button does not appear like the cornered back button, but it appears square. The positing and size remains the same but design of the button changes to square.
Add the following code to “viewDidLoad” method of your view controller file –
1
2
3
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = item;
Add the following function anywhere in the same file –
1
2
3
-(void)back {
// back button code
}
2. The following code is placed in the “viewWillDisappear” function of your view controller file –
1
2
3
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
// back button code
}
This code checks for the self view controller to be present in the memory or not. If not then it assumes the back button is pressed. And the code in curly brackets gets executed.
The benefit of this code is that it does nothing to the back button, so it appears exactly the same as it should be.
Both the techniques are easy to implement. Use the one which suits your needs.

No comments:

Post a Comment