1

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.enter image description here

Nazish Ali
  • 320
  • 3
  • 16
  • 1
    Possible 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 Answers3

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
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.

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/index.html#//apple_ref/doc/uid/TP40012857-UIView-SW7

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