-3

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

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

但这行不通。有人对此有任何想法吗?我很感激任何建议!

4

2 回答 2

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
 }
于 2014-08-29T09:54:51.857 回答
0

试试这个哥们

-(void) viewWillDisappear:(BOOL)animated {

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

        NSLog(@"Back Pressed");

    }
    [super viewWillDisappear:animated];
}

或者

-(void) viewWillDisappear:(BOOL)animated {


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

        NSLog(@"Back Pressed");

    }

    [super viewWillDisappear:animated];
}
于 2014-08-29T09:51:58.180 回答