12

我有两个controllers1st isself和 2nd is maincontroller,我正在推maincontrollerstack,所以后退按钮会自动出现。

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

我怎样才能做到这一点?

4

8 回答 8

20

或者您可以使用UINavigationController的委托方法。willShowViewController按下 VC 的后退按钮时调用该方法。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
于 2013-01-10T10:59:32.350 回答
8

首先使用隐藏后退按钮

self.navigationItem.hidesBackButton = YES;

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

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

你的选择器在这里:

- (void)popAlertAction:(UIBarButtonItem*)sender
{
    //Do ur stuff for pop up
}
于 2013-01-10T10:54:43.367 回答
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];
}
于 2014-09-18T11:32:21.330 回答
2

创建自己的并将UIBarButtonItem其设置leftBarButtonItemviewDidLoad.mainController

例如(这里我使用了一个系统项,但您也可以创建一个不同的项,有关详细信息,请参阅类参考)。

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

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

在哪里

- (void)showAlertView:(id)sender
{
    // alert view here...
}
于 2013-01-10T10:53:30.417 回答
0

添加带有操作的自定义后退按钮并在该操作方法中设置警报。您可以从此处添加自定义后退按钮:http: //www.applausible.com/blog/? p=401

于 2013-01-10T10:49:23.667 回答
0

viewControllerCount - 是保存先前在 UINavigationController 中的 viewController 数量的 var。然后,我们检查viewControllerCount > [viewControllers count]如果是,我们就知道我们会返回(即后退按钮模仿)。

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

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

    viewControllerCount = [viewControllers count];
}
于 2015-06-25T12:36:17.173 回答
0
extension ViewController: UINavigationControllerDelegate {
// when the self != viewcontroller ,it's mean back
     func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
          if self != viewController { 
             // your code
           }
}}
于 2022-02-09T08:19:42.560 回答
-1

创建一个按钮并给出按钮操作如下。

[self alert];

当显示警报时,点击是后

[self.navigationController popViewController];

在这之后,

self.navigationController.LeftBarButton = myButton;

这可能会有所帮助

于 2013-01-10T10:49:50.947 回答