iOS-viewDidLoad在didFinishLaunchingWithOptions委托之前被调用? [英] iOS - viewDidLoad is being called BEFORE the didFinishLaunchingWithOptions delegate?

查看:512
本文介绍了iOS-viewDidLoad在didFinishLaunchingWithOptions委托之前被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



然而,我把一个断点放在我的rootViewController的viewDidLoad方法的第一行,另一个断点在我的代表的第一行didFinishLaunchingWithOptions,



令人惊讶的是,我在应用程序中输入了viewDidLoad方法,然后去了didFinishLaunchingWithOptions,然后再执行一次viewDidLoad方法。



发生了什么事?我认为这种行为是完全错误的。



提前谢谢!





编辑

这里是我的iPad的代表didFinishLaunchingWithOptions方法:

   - (BOOL)应用程序: (UIApplication *)应用程序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];
返回YES;
}

这里是我的iPhone的代表didFinishLaunchingWithOptions方法:

$ b $ (UIApplication *)应用程序didFinishLaunchingWithOptions :( NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden :没有];

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];
返回YES;
}

这两个设备都发生这种奇怪的行为。

解决方案

应用程序:didFinishLaunchingWithOptions:


你应该使用此方法初始化您的应用程序并准备
它运行。在您的应用程序启动后调用
其主nib文件已加载。在此方法调用
时,您的应用程序处于非活动状态。在
之后的某个时刻,此方法返回,调用后续委托方法将
移动到应用程序到活动(前台)状态或背景
状态。


一旦调用了视图属性或方法,那么视图会被视图控制器加载。当这种情况发生时, viewDidLoad 被调用,如果您的窗口 rootViewController 属性设置为您的根ViewController在nib中,那么这是预期的行为。


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.

解决方案

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.

这篇关于iOS-viewDidLoad在didFinishLaunchingWithOptions委托之前被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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