-3

Normally,tap back button would take me to previous view controller, but I want to go to some other view controller when I tap back button .so this is what I do:

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:anyPreviousIndex] animated:YES];
    [super viewWillDisappear:animated];
}

But it wouldn't work. Someone got any idea about it? I appreciate for any suggestion!

Mohit
  • 3,708
  • 2
  • 27
  • 30
John
  • 525
  • 3
  • 14
  • 3
    You can create your own back button, and write your code on the event of that button. – Bhupesh Aug 29 '14 at 09:40
  • Not possible in default Navigation bar back button you have to use UIButton – Mohit Aug 29 '14 at 09:44
  • @GenieWanted Thanks. I have a particular condition,and I have to do this – John Aug 29 '14 at 09:44
  • 2
    did you check [this](http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios), [this](http://stackoverflow.com/questions/1214965/setting-action-for-back-button-in-navigation-controller) or [this](http://stackoverflow.com/questions/5217992/back-button-callback-in-navigationcontroller-in-ios) – Bharat Aug 29 '14 at 09:45
  • @MohitPopat You mean I have to customize my own back button? – John Aug 29 '14 at 09:46
  • I mean you cant use Navigation Bar Back button for that. take UIButton and use it – Mohit Aug 29 '14 at 09:47
  • User expects going to previous view controller by tapping Back and not something else every time. I suggest you rethinking navigation model in your application. – Misha Karpenko Aug 29 '14 at 09:54
  • @Bharat Thanks very much. I customize the back button as the answer said, and it did work,thank you! – John Aug 29 '14 at 09:57
  • Glad it helps.. consider some searching before post in future. – Bharat Aug 29 '14 at 10:00
  • @Bhupesh Thank you,customize back button is a right way – John Aug 29 '14 at 10:01
  • @user3779315 welcome :) and don't forgot to upvote the ans ;) – Bhupesh Aug 29 '14 at 10:07

2 Answers2

2

You can use this code, call this function in your viewWillAppear,

- (void)AddBackButtonForiPhone {

   self.navigationItem.hidesBackButton = YES;

    [backButtonView removeFromSuperview]; // It is UIView

   if (backButtonView) {
       [backButtonView release];
       backButtonView = nil;
   }
    if (gblAppDelegate.portrait) {
       backButtonView = [[UIView alloc] initWithFrame:CGRectMake(10,6,80,30)];
   }else{
       backButtonView = [[UIView alloc] initWithFrame:CGRectMake(10,0,80,30)];
   }

   NSString *strText = @"Back";//self.strPreviousTitle;

   UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(2, 0, 80, 27)];

   [btnBack setTitle:strText forState:UIControlStateNormal];
   [btnBack setTitleEdgeInsets:UIEdgeInsetsMake(0, -16, 0, 0)];
   [btnBack setImageEdgeInsets:UIEdgeInsetsMake(0, -16, 0, 0)];
   [btnBack setImage:[UIImage imageNamed:@"left_arrow"] forState:UIControlStateNormal];
   btnBack.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
   [btnBack addTarget:self action:@selector(btnBack:) forControlEvents:UIControlEventTouchUpInside];

   [backButtonView addSubview:btnBack];

   [btnBack release];

   [self.navigationController.navigationBar addSubview:backButtonView];
}

and in viewWillDisappear use this code.

 - (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [backButtonView removeFromSuperview];
   //[gblAppDelegate ShowMainMenu];
 }

 - (void)btnBack:(UIButton *)sender {
   // write your pop logic here
 }
Bhupesh
  • 2,310
  • 13
  • 33
0

Try this Dude

-(void) viewWillDisappear:(BOOL)animated {

    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {

        NSLog(@"Back Pressed");

    }
    [super viewWillDisappear:animated];
}

OR

-(void) viewWillDisappear:(BOOL)animated {


    if ([self.navigationController.viewControllers containsObject:self] == NO) {

        NSLog(@"Back Pressed");

    }

    [super viewWillDisappear:animated];
}