presentViewController在viewDidLoad中不起作用 [英] presentViewController not working in viewDidLoad

查看:70
本文介绍了presentViewController在viewDidLoad中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中,每次应用程序处于活动状态时,都会显示警报操作:

In this code the alert action is shown every time the app become active:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidBecomeActive:)
                                             name:UIApplicationDidBecomeActiveNotification
                                           object:nil];


}


- (void)applicationDidBecomeActive:(NSNotification *)notification
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Please make your choice"
                                                                         message:@"Would you like a cup of coffee?"
                                                                  preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES"
                                                    style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction *action) {
                                                      NSLog(@"You tapped YES");
                                                  }];

    UIAlertAction *maybeAction = [UIAlertAction actionWithTitle:@"MAYBE"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction *action) {
                                                        NSLog(@"You tapped MAYBE");
                                                    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                       style:UIAlertActionStyleCancel
                                                     handler:nil];

    [alertController addAction:yesAction];
    [alertController addAction:maybeAction];
    [alertController addAction:cancelAction];

    [self presentViewController:alertController animated:YES completion:nil];
}

此外,如果我在 viewDidAppear 方法中移动UIAlertController的代码块,一切都会按预期进行.

Also, everything works as expected if I move the code block of the UIAlertController in viewDidAppear method.

但是如果我将UIAlertController移动到 viewDidLoad :

But if I move the UIAlertController in viewDidLoad :

- (void)viewDidLoad {
    UIAlertController *alertController [...]
    [...]
    [self presentViewController:alertController animated:YES completion:nil];
}

它不起作用.该警报未显示.

it doesn't works. The alert is not shown.

推荐答案

在viewDidLoad中,它当时不是视图层次结构的一部分,因此它被静默忽略,这时在viewWillAppear上已经设置了视图层次结构,因此它起作用的原因.

In viewDidLoad it is not part of the view hierarchy at that time hence it is silently ignored, on viewWillAppear at this time the view hierarchy is already set up hence the reason it works.

这篇关于presentViewController在viewDidLoad中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆