如何在iOS中按“返回”按钮时创建确认弹出窗口? [英] How to create a confirmation Pop Up when pushing Back button in iOS?

查看:110
本文介绍了如何在iOS中按“返回”按钮时创建确认弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有人按下我的iOS应用程序的后退按钮时,我想添加一个弹出窗口,询问用户是否真的想要回来。然后,根据用户的响应,我想撤消操作或继续。我试图在我的视图的viewWillDisappear函数中添加代码然后编写正确的委托但它不起作用,因为它总是更改视图然后显示弹出窗口。我的代码是:

I want to add a pop up when someone pushes the "Back" button of my iOS App, to ask the user if he really wants to come back. Then, depending on the user's response, I would like to undo the action or proceed. I've tried to add the code in the viewWillDisappear function of my view and then write the proper delegate but it doesn't work, because it always change the view and then show the pop up. My code is:

    -(void) viewWillDisappear:(BOOL)animated {
       _animated = animated;
       if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
           UIAlertView *alert_undo = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                                message:@"You could be    loosing information with this action. Do you want to proceed?"
                                                               delegate:self
                                                      cancelButtonTitle:@"Go back"
                                                      otherButtonTitles:@"Yes", nil];
           [alert_undo show];
       }
       else [super viewWillDisappear:animated];
   }

代表实施是:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        [super viewWillDisappear:_animated];
    }
}

这根本不起作用。现在有人有更好的方法吗或者可能出错?

This is not working at all. Does anybody now a better way to do it or what could be wrong?

非常感谢,

推荐答案

感谢您的回答@staticVoidMan!我最后使用了你的代码进行了一些修改。后退按钮无法修改,因此应创建一个额外的按钮并隐藏标准按钮。唯一的问题是新的后退按钮的样式,它不等于标准的样式。最终代码是:

Thanks for your answer, @staticVoidMan! I finally used your code with some modifications. The back button cannot be modified so one should create a additional button and hid the standard one. The only problem is the style of the new "Back" button, which is not equal than the standard one. The final code is:

- (void)viewDidLoad
{

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    self.navigationItem.leftBarButtonItem = bbtnBack;

}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}

这篇关于如何在iOS中按“返回”按钮时创建确认弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆