显示来自viewDidLoad的警报消息

编程入门 行业动态 更新时间:2024-10-21 04:16:15
本文介绍了显示来自viewDidLoad的警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想显示ViewController.m的viewDidLoad()方法而不是viewDidAppear()方法的警报消息.

I want to display a alert message from viewDidLoad() method of ViewController.m instead from viewDidAppear() method.

这是我的代码:

- (void)viewDidLoad { [super viewDidLoad]; //A SIMPLE ALERT DIALOG UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Title" message:@"Enter User Credentials" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel action"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK action"); }]; [alert addAction:cancelAction]; [alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil]; }

我收到此错误:

警告:尝试在视图不在窗口层次结构中的<ViewController: 0x7fbc585a09d0>上显示<UIAlertController: 0x7fbc58448960>!

推荐答案

确定不是错误,问题在于viewDidLoad中的视图层次结构未完全设置.如果使用viewDidAppear,则将设置层次结构.

OK not a bug, the issue is that in viewDidLoad the view hierarchy is not fully set. If you use viewDidAppear, then the hierarchy is set.

如果您确实要在viewDidLoad中调用此警报,可以通过将演示文稿调用包装在此GCD块中以引起轻微的延迟,等待下一个运行循环来执行此操作,但是我建议您不要t(很丑).

If you really want to call this alert in viewDidLoad you can do so by wrapping your presentation call in this GCD block to cause a slight delay, waiting for the next run loop, however I suggest you don't (it's ugly).

dispatch_async(dispatch_get_main_queue(), ^ { [self presentViewController:alert animated:YES completion:nil]; });

更多推荐

显示来自viewDidLoad的警报消息

本文发布于:2023-11-26 18:06:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   消息   viewDidLoad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!