xcode 显示来自 viewDidLoad 的警报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34692154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Alert Message from viewDidLoad
提问by Shivani Arorra
I want to display a alert message from viewDidLoad()method of ViewController.minstead from viewDidAppear()method.
我想从viewDidLoad()方法ViewController.m而不是从viewDidAppear()方法显示警报消息。
Here is my code :
这是我的代码:
- (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];
}
and I am getting this error:
我收到此错误:
Warning: Attempt to present
<UIAlertController: 0x7fbc58448960>on<ViewController: 0x7fbc585a09d0>whose view is not in the window hierarchy!
警告:试图提出
<UIAlertController: 0x7fbc58448960>对<ViewController: 0x7fbc585a09d0>他们的看法是不是在窗口层次!
回答by Woodstock
OK not a bug, the issue is that in viewDidLoadthe view hierarchy is not fully set. If you use viewDidAppear, then the hierarchy is set.
好的不是错误,问题是在viewDidLoad视图层次结构中没有完全设置。如果使用viewDidAppear,则设置层次结构。
If you really want to call this alert in viewDidLoadyou 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).
如果你真的想调用这个警报,viewDidLoad你可以通过将你的演示调用包装在这个 GCD 块中来引起轻微的延迟,等待下一个运行循环,但是我建议你不要(这很丑陋)。
dispatch_async(dispatch_get_main_queue(), ^ {
[self presentViewController:alert animated:YES completion:nil];
});
回答by Akash
to move this call to the viewDidAppear: method.
将此调用移动到 viewDidAppear: 方法。
回答by Mayank Patel
You have to embed a navigation controller and present the controller
您必须嵌入导航控制器并显示控制器
- (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.navigationController presentViewController:alert animated:NO completion:nil];
// [self presentViewController:cameraView animated:NO completion:nil]; //this will cause view is not in the window hierarchy error
}
OR
或者
[self.view addSubview:alert.view];
[self addChildViewController:alert];
[alert didMoveToParentViewController:self];
回答by coarist
Swift 3 iOS 10, I used operation queue to put the block of code that updates the UI onto the main thread.
在 Swift 3 iOS 10 中,我使用操作队列将更新 UI 的代码块放到主线程上。
import UIKit
class ViewController2: UIViewController {
var opQueue = OperationQueue()
override func viewDidLoad() {
super.viewDidLoad()
let alert = UIAlertController(title: "MESSAGE", message: "HELLO WORLD!", preferredStyle: UIAlertControllerStyle.alert)
// add an action (button, we can add more than 1 buttons)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
// show the alert
self.opQueue.addOperation {
// Put queue to the main thread which will update the UI
OperationQueue.main.addOperation({
self.present(alert, animated: true, completion: nil)
})
}
}
}
In short we are using async. This allows the alert message to be displayed as expected (even when we are in viewDidLoad()).
简而言之,我们正在使用异步。这允许按预期显示警报消息(即使我们在 viewDidLoad() 中)。

