2015-05-19 65 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

嗯...你是否100%肯定警报的处理程序正在执行,没有按下OK?也许你的代码中有别的东西在执行这些动作... –

+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

删除dismiss块,然后将点击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];