2015-05-19 4 views
1

У меня проблема с . работает отлично, но этого просто не будет. У меня есть следующее:UIAlertController немедленно выполняет обработчик

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(@"Success") 
                        message:LocalizedString(@"Example") preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:LocalizedString(@"Ok") 
                   style:UIAlertActionStyleDefault 
                  handler:^(UIAlertAction *action){ 
                   [self dismissViewControllerAnimated:YES completion:^ 
                   { 
                    [self performSelector:@selector(presentLogInViewController) withObject:nil]; 
                   }]; 
                  }]; 
[alertController addAction:actionOk]; 
[self presentViewController:alertController animated:YES completion:nil]; 

Сообщение отображается в течение двух секунд и выполняет обработчик немедленно. Я хочу обработать, когда нажата кнопка Ok, но она не работает. Что я делаю не так?

+0

Hm ... Вы на 100% уверены, что обработчик предупреждения выполняется без нажатия ОК? Возможно, есть что-то еще в вашем коде в другом месте, которое выполняет эти действия ... –

+0

Да, я уверен. У меня есть делегат для AlertView, но у меня все проверено. AlertView работает, это не – Flipper

+0

Как вы уверены? Что конкретно вы сделали, чтобы проверить? –

ответ

1

Пожалуйста, попробуйте с этим кодом

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) 
             { 
      [alertController dismissViewControllerAnimated:YES completion:nil]; 
             }]; 
     [alertController addAction:cancelAction]; 
     [self presentViewController: alertController animated:YES completion:nil]; 
+0

То же самое, сообщение за две секунды и оно уходит – Flipper

0

Вы не должны отклонить оповещения контроллер внутри блока завершения, потому что он уволит автоматически.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" 
            message:@"This is an alert." 
            preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
     handler:^(UIAlertAction * action) { 
    // here you can add the completion for when the OK button is pressed 
}]; 
    [alert addAction:defaultAction]; 
    [self presentViewController:alert animated:YES completion:nil]; 

Для каждого действия, добавляемого с помощью метода addAction:, предупреждение контроллер настраивает кнопку с деталями действий. Когда пользователь отбирает это действие, контроллер предупреждения выполняет блок, который вы предоставили при создании объекта действия.

2

Удалить блок увольнения, а затем обработчик будет вызван нажатием кнопки OK.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(@"Success") 
                     message:LocalizedString(@"Example") 
                    preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *actionOk = [UIAlertAction actionWithTitle:LocalizedString(@"Ok") 
                style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action) { 
                 [self performSelector:@selector(presentLogInViewController) withObject:nil];                 
               }]; 

[alertController addAction:actionOk]; 
[self presentViewController:alertController animated:YES completion:nil]; 
Смежные вопросы