У меня есть этот код, где я хочу отображать сообщение с ответом ДА или НЕТ, используя UIAlertAction; это мой код:Как получить ответ UIAlertAction (UIAlertController) от обработчика блока?
-(void) displayAlert: (NSString *)alertTitle andMessage: (NSString *)alertMessage andViewController: (UIViewController *) viewController andTag:(int)tag {
// create controller
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// add buttons
if(tag == 0) { // ok/cancel response
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
}
else if(tag == 1) { // yes/no response
UIAlertAction *noAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"No", @"No action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
UIAlertAction *yesAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Yes", @"Yes action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
}
// display controller
[viewController presentViewController: alertController animated:YES completion:nil];
}
Моя проблема заключается в том, что блок не разрешает возврат (в соответствии с docs). Итак, как мне вернуть ответ на вызывающий метод? (он не встроен, он находится в классе общих методов, потому что он используется часто).
Спасибо ... Я ценю ваше время ... – SpokaneDude
Можете ли вы добавить пример, который показывает, как передать обработчик в качестве параметра? – Satyam
@Satyam: В исходном вопросе есть код 'UIAlertAction actionWithTitle', который в основном делает то же самое. – sha