I have a screen having navigation bar, back button default is placed using navigationItem storyboard. I want to popup view controller on back button based on condition. Back button is default before Add Event. Please suggest a solution for this problem.
Asked
Active
Viewed 711 times
1

Nazish Ali
- 320
- 3
- 16
-
1Possible duplicate of [Execute action when back bar button of UINavigationController is pressed](http://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button-of-uinavigationcontroller-is-pressed) – Jigar Tarsariya Jun 07 '16 at 11:35
3 Answers
2
You should implement this method,
-(void)willMoveToParentViewController:(UIViewController *)parent{
if (parent == nil) {
// handle your back button's task here
}
}
This method get called before navigation to parentview
controller.
If it's not work in your case then you can use custom back button something like,
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem=newBackButton;
}
-(void)goBack:(UIBarButtonItem *)sender {
//Handle your back button's task here and you have to call popViewcontroller your self in this case
}

Ketan Parmar
- 27,092
- 9
- 50
- 75
-
I want to show alert, after action on alert then pop view controller. – Nazish Ali Jun 07 '16 at 11:34
-
-
Then add custom button with clear color and don't set title. Just add button with clear color without border. so original naviation bar button will visible and click detect on custom button – Ketan Parmar Jun 07 '16 at 12:35
0
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem=newBackButton;
-(void)goBack:(UIBarButtonItem *)sender {
UIAlertView *removeAddAlertView = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@“Are you want to go Back ” delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
[removeAddAlertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// cancel
}else if (buttonIndex == 1) {
// ok
}
}

Jigar Tarsariya
- 3,189
- 3
- 14
- 38

Jagveer Singh
- 2,258
- 19
- 34
0
If you set the tintColor Of NavigationBar,add a custom back button image without title that tint color will reflect the image color. Please follow this apple documentaion link.
UINavigationItem *navItem = [[UINavigationItem alloc] init]; navBar.tintColor = self.tintColor;
UIImage *myImage = [UIImage imageNamed:@"left_arrow.png"]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonFunction:)];
navItem.leftBarButtonItem = leftButton;
navBar.items = @[ navItem ];

Nazish Ali
- 320
- 3
- 16