繁体   English   中英

当按下UINavigationController上的后退按钮时,如何调用方法? (苹果手机)

[英]How to call a method when back button on a UINavigationController is pressed? (iPhone)

当按下UINavigationController上的后退按钮时,如何调用方法。 我需要能够在显示上一个视图之前调用一个方法。 这可能吗? 如果是这样,有人可以建议如何做吗?

一种快速的解决方案是为viewWillDisappear:方法添加一个实现。 一旦viewController响应后退按钮的按下而消失,它将被触发。

- (void)viewWillDisappear:(BOOL)animated {
  //... 
  //make you stuff here
  //... 
}

另一种解决方案是将自定义响应器添加到“后退”按钮。 您可以按如下方式修改viewController的init方法:

- (id)init {
    if (self = [super init]) {
        //other your stuff goes here
        //... 
        //here we customize the target and action for the backBarButtonItem
        //every navigationController has one of this item automatically configured to pop back
        self.navigationItem.backBarButtonItem.target = self;
        self.navigationItem.backBarButtonItem.action = @selector(backButtonDidPressed:);
    }
    return self;
}

然后可以使用以下选择器方法。 确保正确关闭viewController,否则导航控制器将不会弹出。

- (void)backButtonDidPressed:(id)aResponder {
    //do your stuff
    //but don't forget to dismiss the viewcontroller
    [self.navigationController popViewControllerAnimated:TRUE];
}
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 52, 31)];
[backButton setBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
//[backButton setTitle:@"CLOSE" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
[backButton addTarget:self action:@selector(tappedBackButton:) forControlEvents:UIControlStateHighlighted];

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:backButton];

self.navigationItem.leftBarButtonItem = item;
[item release];

创建一个自定义的后退按钮,并使用该选择器调用所需的方法。 并且还使用[self.navigationController popViewControllerAnimated:TRUE];

- (void)tappedBackButton:(id)button
{
    // call your method here
    [self.navigationController popViewControllerAnimated:TRUE];
} 

我认为我找到了一种“干净”的方法:

首先,将您的视图控制器设置为符合UINavigationControllerDelegate协议

@interface MyViewController () <UINavigationControllerDelegate>

然后,将其定义为委托

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Set this View Controller as the navigation controller delegate
    self.navigationController.delegate = self;

}

最后,使用UINavigationControllerDelegate协议中的此方法:

#pragma mark - Navigation controller delegate
- (void)willMoveToParentViewController:(UIViewController *)parent
{
    // If there is no parent, then it means that the view controller has been removed from the stack, which happens when the back button has been pressed
    if (!parent) {
        NSLog(@"Back button pressed");

        // it can be useful to store this into a BOOL property
        self.backButtonPressed = YES;
    }
}

最简单的方法是将代码放入ViewController中,该代码将在按下后退按钮时显示。 您可以使用viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     //Your code here
}

请注意,由于任何其他原因在显示视图时也会执行此操作,因此,如果只希望在按下后退按钮时执行此视图,则必须使用委托: UINavigationControllerDelegate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM