转到Tap导航后退按钮上的其他ViewController

时间:2014-08-29 09:36:00

标签: ios uinavigationcontroller back-button poptoviewcontroller

通常情况下,点击后退按钮会将我带到上一个视图控制器,但是当我点按后退按钮时,我想转到其他视图控制器。这就是我所做的:

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

但它不起作用。 有人对此有所了解吗?我很感激任何建议!

2 个答案:

答案 0 :(得分:2)

您可以使用此代码,在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];
}

并在viewWillDisappear中使用此代码。

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

 - (void)btnBack:(UIButton *)sender {
   // write your pop logic here
 }

答案 1 :(得分:0)

试试这个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];
}