显示来自 viewDidLoad 的警报消息

Display Alert Message from viewDidLoad

我想显示来自 ViewController.mviewDidLoad() 方法而不是 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!

您必须嵌入导航控制器并显示控制器

- (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

}

  [self.view addSubview:alert.view];
  [self addChildViewController:alert];
  [alert didMoveToParentViewController:self];

将此调用移至 viewDidAppear: 方法。

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

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

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

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)
            })
        }
    }
}

简而言之,我们正在使用异步。这允许警报消息按预期显示(即使我们在 viewDidLoad() 中)。