UINavigationController和后退按钮动作

Har*_*ran 12 objective-c uinavigationcontroller ios

我有两个controllers第一个self和第二个maincontroller,我maincontroller堆栈中,所以后退按钮自动进入.

在这里,我需要在用户按下后退按钮时发出警报.

我怎样才能做到这一点?

Sat*_*ran 20

或者你可以使用UINavigationController's委托方法.willShowViewController按下VC的后退按钮时会调用该方法.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
Run Code Online (Sandbox Code Playgroud)


Sat*_*zad 8

首先使用隐藏后退按钮

self.navigationItem.hidesBackButton = YES;
Run Code Online (Sandbox Code Playgroud)

然后创建自己的自定义按钮:

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

你的选择器在这里:

- (void)popAlertAction:(UIBarButtonItem*)sender
{
    //Do ur stuff for pop up
}
Run Code Online (Sandbox Code Playgroud)


Xar*_*mer 7

最好最简单的方法

尝试将其放入要检测印刷机的视图控制器中:

-(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];
}
Run Code Online (Sandbox Code Playgroud)