iOS - viewDidLoad is being called BEFORE the didFinishLaunchingWithOptions delegate?

会有一股神秘感。 提交于 2020-01-01 18:18:11

问题


I have an application that is working as it was supposed to.

However, I put one breakpoint at the first line of my rootViewController's viewDidLoad method and another breakpoint in the first line of my delegate's didFinishLaunchingWithOptions,

Surprisingly for me, the application entered in the viewDidLoad method, then went to the didFinishLaunchingWithOptions, and then executed one more time the viewDidLoad method.

What is going on? I think that that behavior is totally wrong.

Thank you in advance!

# Edited

Here goes my iPad's delegate didFinishLaunchingWithOptions method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    UtilXML *utilXML = [[UtilXML alloc] init];
    [utilXML startXMLCommunication];
    int quantidadeDeComicsBaixadas = [utilXML quantidadaDeComicsBaixadas];

    main_iPad *mainiPad = [[main_iPad alloc] init];
    mainiPad.quantidadeDeComicsBaixadas = quantidadeDeComicsBaixadas;
    mainiPad.navigationItem.title = @"TitleFirstScreen";

    UIBarButtonItem *botaoSobre = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:mainiPad action:@selector(goToAboutView)];
    mainiPad.navigationItem.rightBarButtonItem = botaoSobre;

    navController = [[UINavigationController alloc] initWithRootViewController:mainiPad];
    navController.navigationBar.tintColor = [UIColor orangeColor];
    navController.navigationBar.translucent = YES;


    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

And here is my iPhone's delegate didFinishLaunchingWithOptions method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    UtilXML *utilXML = [[UtilXML alloc] init];
    [utilXML startXMLCommunication];
    int quantidadeDeComicsBaixadas = [utilXML quantidadaDeComicsBaixadas];

    main_iPhone *mainiPhone = [[main_iPhone alloc] init];
    mainiPhone.quantidadeDeComicsBaixadas = quantidadeDeComicsBaixadas;
    mainiPhone.navigationItem.title = @"TitleFirstScreen";

    UIBarButtonItem *botaoSobre = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:mainiPhone action:@selector(goToAboutView)];
    mainiPhone.navigationItem.rightBarButtonItem = botaoSobre;

    navController = [[UINavigationController alloc] initWithRootViewController:mainiPhone];
    navController.navigationBar.tintColor = [UIColor orangeColor];
    navController.navigationBar.translucent = YES;

    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

This strange behavior occurs in both devices.


回答1:


The documentation for application:didFinishLaunchingWithOptions:

You should use this method to initialize your application and prepare it for running. It is called after your application has been launched and its main nib file has been loaded. At the time this method is called, your application is in the inactive state. At some point after this method returns, a subsequent delegate method is called to move your application to the active (foreground) state or the background state.

So the view is lazily loaded for view controllers once the the view property or method is called. When this happens viewDidLoad is called so if your window rootViewController property is set to your root ViewController in the nib then this is expected behavior.



来源:https://stackoverflow.com/questions/7030635/ios-viewdidload-is-being-called-before-the-didfinishlaunchingwithoptions-deleg

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