【问题标题】:Return button pressed in UIAlertController在 UIAlertController 中按下返回按钮
【发布时间】:2015-04-15 10:54:11
【问题描述】:

我正在尝试将UIAlertController 用于多种用途。它有两个按钮,取消和确定。我想将它添加到一个方法并返回按钮按下,所以我可以检查用户的响应并采取行动。

现在,我无法返回 block 内的值。那么,我该怎么做呢?

谢谢。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Atenção!", "Atenção!") message:NSLocalizedString(@"Você não finalizou a sua série. Se sair desta tela, irá zerar o cronômetro.", "") preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelar = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancelar", "Cancelar") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   // I would like to return this button press to the method calling this one. 
                               }];
    [alertController addAction:cancelar];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", "OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   // I would like to return this button press to the method calling this one.
                               }];
    [alertController addAction:ok];

    [self presentViewController:alertController animated:YES completion:nil];

更新:实际使用

当用户按下back button时,它会调用一个方法来检查一个条件。如果满足条件,则会显示警报,用户需要决定是否离开屏幕。这就是为什么将答案返回给IBActionback Button 会很棒的原因。

注意:整个想法是有其他方法,除了back Button,也显示警报并获得用户的响应。

【问题讨论】:

  • 为什么不在后退按钮中使用这个 AlertController?这个警报代码是用其他方法而不是在返回按钮中编写的吗?
  • 那行得通。但是如果我想在其他按钮上使用alertController?我将不得不重复代码或将alert 保存在property 中。对?这似乎不是最好的解决方案。
  • 您可以使用我在回答中显示的块。

标签: ios objective-c swift uialertcontroller


【解决方案1】:

你可以利用积木。

创建这样的方法

- (void)alertWithResponse:(void (^)(BOOL didCancel))response {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Atenção!", "Atenção!") message:NSLocalizedString(@"Você não finalizou a sua série. Se sair desta tela, irá zerar o cronômetro.", "") preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelar = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancelar", "Cancelar") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   response(YES);
                               }];
    [alertController addAction:cancelar];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", "OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   response(NO);
                               }];
    [alertController addAction:ok];

    [self presentViewController:alertController animated:YES completion:nil];
}

现在在你的后退按钮中,像这样调用这个方法

- (IBAction)backButtonCliccked:(id)sender {

    //your button logic...
    //.
    //.
    //.
    [self alertWithResponse:^(BOOL didCancel) {
        if(didCancel) {
            //alert returned Cancel
        } else {
            //alert returned OK
        }
    }];
}

【讨论】:

  • 与旧的 UIAlertView 相比,我在回调内部运行的块有相当大的延迟。这真的很烦人。它似乎“按预期中断”,因为它现在等待动画完成。似乎找不到解决这个问题的好方法。
  • 能否详细解释一下您面临的问题是什么?可能是您没有在主队列中进行 UI 更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多