iOS self.window-什么时候创建的? [英] iOS self.window - when is it created?

查看:183
本文介绍了iOS self.window-什么时候创建的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用单视图模板启动应用程序,并在AppDelegate.m的application: didFinishLaunchingWithOptions:方法的第一行中添加NSLog(@"self.window = %@", self.window);时,您可以看到self.window存在于您的应用程序中.

但是,当您使用空模板启动应用程序并尝试将self.window登录到控制台时,结果将返回null.即使您添加情节提要和视图控制器,并将其视图控制器设置为初始视图控制器,并尝试记录self.window,结果也是相同的-其值设置为null.

请注意,无论采取哪种方式,默认情况下都可以在AppDelegate.h中声明为@property (strong, nonatomic) UIWindow *window;.因此,我想知道为什么在第一种情况下可以看到self.window已初始化并设置了值,而在后一种情况下却没有.另外,如果self.window在第一种情况下已经声明并初始化,而在第二种情况下没有声明和初始化,那么如何找到初始化代码?

在两种情况下,@property声明看起来都是相同的-正如我提到的,在两种情况下,我都尝试在AppDelegate.mself.window的值>方法.

那么我想念的东西是什么?尽管我在代码和情节提要上都没有发现任何差异,但我不知道为什么这两种情况的行为会有所不同.

我使用iOS 7和Xcode5.谢谢.

解决方案

好,当您创建带有情节提要或Nib的项目时,项目设置将告诉项目情节提要/笔尖是主界面". /p>

这将触发应用程序在启动时加载该接口.这就是在这种情况下创建self.window的原因.

创建空应用程序时,没有任何界面可设置为主界面.

然后您需要像这样自己创建窗口...

-(void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     UIViewController *someController = [UIViewController... //create your initial controller

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
}

反正这样.已经有一段时间了.

或者,如果您创建一个空应用程序,然后添加要用作初始笔尖的笔尖文件,则可以在项目设置中选择它.

在一般目标中.在部署信息"部分中,从项目的笔尖中选择主界面".然后,将在应用程序启动时加载该笔尖.

when you start your app using single view template, and you add the NSLog(@"self.window = %@", self.window); in your first line of the AppDelegate.m's application: didFinishLaunchingWithOptions: method, you can see that self.window exists in your app.

However, when you start your app using empty template, and tried to log the self.window to the console, the result returns null. Even if you add storyboard and a view controller, and set its view controller as the initial view controller, and attempt to log the self.window, the result is the same - its value is set to null.

And note that whichever way you take, you can find you declare @property (strong, nonatomic) UIWindow *window; in AppDelegate.h by default. So I wonder why in the first case, you can see that self.window is initialized and set the value but not in the latter case. Also, if self.window is already declared and initialized in the first case but NOT in the second case, how can I find the initialization code?

It looks like in both cases, the @property declaration is same - and in both cases, as I mentioned, I tried to log the value of self.window in the FIRST LINE of the AppDelegate.m's application: didFinishLaunchingWithOptions: method.

So anything that I'm missing? I don't know why those two cases act differently despite me not finding any differences in both code and storyboard.

I use iOS 7 and Xcode 5. Thanks.

解决方案

OK, when you create a project with a Storyboard or Nib then the project settings will tell the project that the storyboard/nib is the "Main Interface".

This triggers the application to load that interface on start up. This is why the self.window is created in these cases.

When you create an empty application there is no interface to set as the main interface.

You then need to create the window yourself like this...

-(void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     UIViewController *someController = [UIViewController... //create your initial controller

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
}

Something like this anyway. It's been a while.

Alternatively, if you create an empty application and then add a nib file that you want to use as the initial nib then you can select it in the project settings.

In the Target in General. In the section "Deployment Info" select the "Main Interface" from the nibs in your project. This will then load that nib when the application starts.

这篇关于iOS self.window-什么时候创建的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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