【发布时间】: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