UINavigationController and back button action

2019-02-12 05:49发布

I have an two controllers 1st is self and 2nd is maincontroller, where I'm pushing maincontroller in stack, so the back button is automatically coming.

Here I need to make an alert when the user presses the back button.

How can I do this?

7条回答
Luminary・发光体
2楼-- · 2019-02-12 06:10

First hide the back button by using

self.navigationItem.hidesBackButton = YES;

and then create your own Custom Button:

UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backBtn;
[backBtn release];

and your selector is here:

- (void)popAlertAction:(UIBarButtonItem*)sender
{
    //Do ur stuff for pop up
}
查看更多
Fickle 薄情
3楼-- · 2019-02-12 06:13

Or you can use the UINavigationController's delegate methods. The method willShowViewController is called when the back button of your VC is pressed.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
查看更多
ら.Afraid
4楼-- · 2019-02-12 06:17

Create your own UIBarButtonItem and set it as the leftBarButtonItem in viewDidLoad method of mainController.

For example (here I used a system item but you can also create a different one, see class reference for details).

UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAlertView:)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;

// only if you don't use ARC
// [leftBarButtonItem release];

where

- (void)showAlertView:(id)sender
{
    // alert view here...
}
查看更多
祖国的老花朵
5楼-- · 2019-02-12 06:23

Best and Easiest way

Try putting this into the view controller where you want to detect the press:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // back button was pressed.  We know this is true because self is no longer
       // in the navigation stack.  
    }
    [super viewWillDisappear:animated];
}
查看更多
仙女界的扛把子
6楼-- · 2019-02-12 06:25

viewControllerCount - is the var that holds the number of viewControllers previously was in the UINavigationController. Then, we check if viewControllerCount > [viewControllers count] if so, we know that we will get back (i.e. Back button imitation).

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    NSArray *viewControllers = [navigationController viewControllers];

    if (viewControllerCount > [viewControllers count])
    {
        // your code
    }

    viewControllerCount = [viewControllers count];
}
查看更多
可以哭但决不认输i
7楼-- · 2019-02-12 06:28

create a button and give the button action as follows.

[self alert];

and when the alert is displayed, after tapping over yes

[self.navigationController popViewController];

after this,

self.navigationController.LeftBarButton = myButton;

this may help

查看更多
登录 后发表回答