Display Alert Message from viewDidLoad

后端 未结 4 1371
孤街浪徒
孤街浪徒 2021-01-08 01:01

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

Here is m

4条回答
  •  礼貌的吻别
    2021-01-08 01:09

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

提交回复
热议问题