Return 在 UIAlertController 中按下按钮
Return button pressed in UIAlertController
我正在尝试将 UIAlertController
用于多种用途。它有两个按钮,取消和确定。我想将它添加到一个方法中,然后 return 按下按钮,这样我就可以检查用户的响应并对其采取行动。
现在,我无法 return 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
时,它会调用一个方法来检查条件。如果满足条件,则会显示警报,用户需要决定是否离开屏幕。这就是 return IBAction
back Button
的答案会很棒的原因。
注意:整个想法是除了 back Button
之外还有其他方法,也显示警报并获得用户的响应。
你可以使用积木。
像这样创建一个方法
- (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
}
}];
}
我正在尝试将 UIAlertController
用于多种用途。它有两个按钮,取消和确定。我想将它添加到一个方法中,然后 return 按下按钮,这样我就可以检查用户的响应并对其采取行动。
现在,我无法 return 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
时,它会调用一个方法来检查条件。如果满足条件,则会显示警报,用户需要决定是否离开屏幕。这就是 return IBAction
back Button
的答案会很棒的原因。
注意:整个想法是除了 back Button
之外还有其他方法,也显示警报并获得用户的响应。
你可以使用积木。
像这样创建一个方法
- (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
}
}];
}