UINavigationController and back button action

后端 未结 7 2035
-上瘾入骨i
-上瘾入骨i 2021-01-04 09:09

I have an two controllers 1st is self and 2nd is maincontroller, where I\'m pushing maincontroller in stack

相关标签:
7条回答
  • 2021-01-04 09:16

    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...
    }
    
    0 讨论(0)
  • 2021-01-04 09:19

    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];
    }
    
    0 讨论(0)
  • 2021-01-04 09:19

    add a custom back button with an action and set your alert in that action method.You can add your custom back button from here: http://www.applausible.com/blog/?p=401

    0 讨论(0)
  • 2021-01-04 09:22

    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
    }
    
    0 讨论(0)
  • 2021-01-04 09:34

    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;
    
    0 讨论(0)
  • 2021-01-04 09:35

    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

    0 讨论(0)
提交回复
热议问题