gpt4 book ai didi

ios - 显示来自 viewDidLoad 的警报消息

转载 作者:技术小花猫 更新时间:2023-10-29 10:31:27 24 4
gpt4 key购买 nike

我想显示来自 viewDidLoad() 的警告消息ViewController.m的方法|而不是 viewDidAppear()方法。

这是我的代码:

- (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];
}

我收到这个错误:

Warning: Attempt to present <UIAlertController: 0x7fbc58448960> on <ViewController: 0x7fbc585a09d0> whose view is not in the window hierarchy!

最佳答案

好的,这不是错误,问题是在 viewDidLoad 中, View 层次结构没有完全设置。如果您使用 viewDidAppear,则会设置层次结构。

如果你真的想在 viewDidLoad 中调用这个警报,你可以通过将你的演示调用包装在这个 GCD block 中来实现,从而造成轻微的延迟,等待下一个运行循环,但是我建议你不要(它很难看)。

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

关于ios - 显示来自 viewDidLoad 的警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com