【问题标题】:UIAlertController not displaying Alert in iOS 8.4.1UIAlertController 在 iOS 8.4.1 中不显示警报
【发布时间】:2015-10-08 11:15:20
【问题描述】:

我已经使用

展示了视图控制器
[self presentViewController:viewController animated:YES completion:nil];

在呈现的视图控制器中,我试图呈现 UIAlertcontroller,它没有呈现也在 iOS 8.4.1 的控制台中显示警告

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertAction *extend_action = nil;
    UIAlertAction *ok_action  = nil;

    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Call will begin"
                                                                        message:msg_str   
                                                                 preferredStyle:UIAlertControllerStyleAlert];

    extend_action = [UIAlertAction  actionWithTitle:@"Extend Time?" 
                                              style:UIAlertActionStyleDefault
                                            handler:^(UIAlertAction *action) { 
                                                 // Method.
                                            }];
    ok_action = [UIAlertAction actionWithTitle:@"Ok" 
                                         style:UIAlertActionStyleDefault                            
                                       handler:^(UIAlertAction *action) { 
                                    }];                        
    [controller addAction:extend_action];
    [controller addAction:ok_action];
    [self presentViewController:controller animated:YES completion:nil];
});

尝试在其视图不在窗口层次结构中呈现 UIAlertController! 我得到了警告。

【问题讨论】:

  • 如消息所述,您试图将警报呈现到不可见的视图中。
  • 您是如何创建警报的?你能显示你的UIAlertController 代码和你添加到UIAlertControllerUIAlertActions 吗?
  • @DogCoffee,那么我需要在呈现警报控制器时进行修改。
  • 需要更多代码,为什么不升级到 iOS 9 并使用 Swift??
  • 看起来不错,我唯一能想到的是,当 VC A 在屏幕上时,这是在 VC B 中调用的(例如)——你从哪里调用这个方法?在模态解雇或其他什么??

标签: ios objective-c uiviewcontroller uialertcontroller


【解决方案1】:

尝试在主队列中展示它:

dispatch_async(dispatch_get_main_queue(), {
    self.presentViewController(alertController, animated: true, completion: nil)
})

【讨论】:

  • 这很快,我使用了objective-C。它不工作。我收到相同的警告消息,但我的警报控制器没有出现。
  • 请发布一些代码显示 UIAlertController 的创建
  • 您的代码运行良好。我尝试过这个。顺便问一下你在哪里写这段代码?
  • 我正在调用我自己的方法,我知道了它不显示的原因,因为第一个视图控制器出现在那个视图上,我试图再显示一个视图(警报控制器),所以它没有呈现。
  • 调用时self.view不在屏幕上时出现此错误
【解决方案2】:

您需要延迟或在主线程中调用您的方法以显示警报控制器 这是一个例子

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor greenColor];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self presentAlert];
});

}

- (void)presentAlert
{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Info" message:@"Hello alet controller" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIActionSheetStyleDefault handler:^(UIAlertAction *action) {
    [alert dismissViewControllerAnimated:YES completion:nil];
}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIActionSheetStyleDefault handler:^(UIAlertAction *action) {
    [alert dismissViewControllerAnimated:YES completion:nil];
}];

[alert addAction:ok];
[alert addAction:cancel];

  [self presentViewController:alert animated:YES completion:nil];

}

你可以使用

dispatch_async(dispatch_get_main_queue(), ^{
[self presentAlert];
});

希望对你有所帮助...

【讨论】:

    【解决方案3】:

    您必须在 viewDidAppear 方法上显示警报。在 viewDidLoad 方法中,视图尚未出现,这就是您的警报变得可见的原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多