iOS - viewDidLoad is being called BEFORE the didFi

2020-07-27 04:32发布

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条回答
放我归山
2楼-- · 2020-07-27 05:10

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.

查看更多
登录 后发表回答