Display Alert Message from viewDidLoad

青春壹個敷衍的年華 提交于 2019-12-01 02:47:51

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.

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

to move this call to the viewDidAppear: method.

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

Swift 3 iOS 10, I used operation queue to put the block of code that updates the UI onto the main thread.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!