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

时间:2021-06-11 00:04:46

How can I call a method when the back button on a UINavigationController is pressed. I need to be able to call a method before the previous view is displayed. Is this possible? If so please can someone advise on how to do this?

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

4 个解决方案

#1


15  

A fast solution is to add an implementation for the viewWillDisappear: method. It will be triggered as soon as the viewController will disappear in response to the back button pression.

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

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

Another solution is to add a custom responder to the back button. You can modify your viewController's init method as follow:

另一种解决方案是在后退按钮上添加自定义响应器。您可以修改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;
}

and then you can use a selector method as the following. Be sure to dismiss correctly the viewController, otherwise your navigation controller won't pop as wanted.

然后您可以使用选择器方法如下。请务必正确关闭viewController,否则导航控制器将无法按需弹出。

- (void)backButtonDidPressed:(id)aResponder {
    //do your stuff
    //but don't forget to dismiss the viewcontroller
    [self.navigationController popViewControllerAnimated:TRUE];
}

#2


6  

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];

Create a custom Back Button and using that selector call a method that you want. And also use [self.navigationController popViewControllerAnimated:TRUE];

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

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

#3


6  

I think I found a "clean" way to do that :

我想我发现了一种“干净”的方法:

First, set your view controller as complying with the UINavigationControllerDelegate protocol

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

@interface MyViewController () <UINavigationControllerDelegate>

Then, define it as the delegate

然后,将其定义为委托

- (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;

}

Last, use this method from the UINavigationControllerDelegate protocol :

最后,使用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;
    }
}

#4


1  

The easiest way would be, to put the code in the ViewController that will be presented when the back button is pressed. You can use viewWillAppear

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

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

Note that this will also be executed when the view is presented for any other reason, so if you want it to happen only when the back button is pressed you have to use the delegate: UINavigationControllerDelegate

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

#1


15  

A fast solution is to add an implementation for the viewWillDisappear: method. It will be triggered as soon as the viewController will disappear in response to the back button pression.

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

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

Another solution is to add a custom responder to the back button. You can modify your viewController's init method as follow:

另一种解决方案是在后退按钮上添加自定义响应器。您可以修改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;
}

and then you can use a selector method as the following. Be sure to dismiss correctly the viewController, otherwise your navigation controller won't pop as wanted.

然后您可以使用选择器方法如下。请务必正确关闭viewController,否则导航控制器将无法按需弹出。

- (void)backButtonDidPressed:(id)aResponder {
    //do your stuff
    //but don't forget to dismiss the viewcontroller
    [self.navigationController popViewControllerAnimated:TRUE];
}

#2


6  

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];

Create a custom Back Button and using that selector call a method that you want. And also use [self.navigationController popViewControllerAnimated:TRUE];

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

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

#3


6  

I think I found a "clean" way to do that :

我想我发现了一种“干净”的方法:

First, set your view controller as complying with the UINavigationControllerDelegate protocol

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

@interface MyViewController () <UINavigationControllerDelegate>

Then, define it as the delegate

然后,将其定义为委托

- (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;

}

Last, use this method from the UINavigationControllerDelegate protocol :

最后,使用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;
    }
}

#4


1  

The easiest way would be, to put the code in the ViewController that will be presented when the back button is pressed. You can use viewWillAppear

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

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

Note that this will also be executed when the view is presented for any other reason, so if you want it to happen only when the back button is pressed you have to use the delegate: UINavigationControllerDelegate

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