【问题标题】:How to create a confirmation Pop Up when pushing Back button in iOS?在iOS中按下后退按钮时如何创建确认弹出窗口?
【发布时间】:2014-01-09 18:50:32
【问题描述】:

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

    -(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];
    }
}

这根本不起作用。现在有没有人有更好的方法来做到这一点或可能出了什么问题?

非常感谢,

【问题讨论】:

    标签: ios objective-c popup viewcontroller


    【解决方案1】:

    一旦调用 -viewWillDisappear:,就无法阻止您的 viewController 消失。

    理想情况下,您应该覆盖navigationBar 的后退按钮,并在其方法中显示警报(其余部分几乎相同

    - (void)viewDidLoad
    {
        //...
        UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                     style:UIBarButtonItemStyleBordered
                                                                    target:self
                                                                    action:@selector(goBack:)];
    
        [self.navigationItem setBackBarButtonItem: 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;
        }
    }
    

    注意:不要忘记在这个viewController.h文件中声明<UIAlertViewDelegate> em>

    【讨论】:

    • @recasens :不客气。将您的答案标记为已接受,我也会赞成 :) 无论如何,从技术上讲...[self.navigationItem setLeftBarButtonItem:bbtnBack];[self.navigationItem setBackBarButtonItem: bbtnBack]; 都应该有效(在您的情况下
    【解决方案2】:

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

    - (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;
        }
    }
    

    【讨论】:

    • @AlexSpencer 请避免编辑帖子以用您自己的想法改进代码。您可以添加评论以提出改进建议。请参阅此处的指南:stackoverflow.com/help/editing
    • @SysDragon 这是一个微妙的变化。但这不是我的意见。见此链接:stackoverflow.com/questions/4649423/…
    • @AlexSpencer 这是在谈论良好的编程实践,而不是在编辑其他内容。在此站点中,您应该编辑帖子以添加新代码以进行改进。您可以在评论中提出建议。
    • @SysDragon 好吧,我不会编辑代码我会评论。话虽如此,这是我对上面代码的评论:在 switch case 语句中总是有一个default: break;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多