iOS self.window - 何时创建? [英] iOS self.window - when is it created?

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

问题描述

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

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.

但是,当您使用空模板启动应用程序并尝试将 self.window 记录到控制台时,结果返回 null.即使你添加 storyboard 和一个视图控制器,并将它的视图控制器设置为初始视图控制器,并尝试记录 self.window,结果是一样的 - 它的值被设置为 null.

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.

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

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?

看起来在这两种情况下,@property 声明是相同的 - 在这两种情况下,正如我提到的,我试图记录 self.window 的值在 AppDelegate.mapplication: didFinishLaunchingWithOptions: 方法的第一行中.

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.

我使用 iOS 7 和 Xcode 5.谢谢.

I use iOS 7 and Xcode 5. Thanks.

推荐答案

好的,当你使用 Storyboard 或 Nib 创建项目时,项目设置会告诉项目 storyboard/nib 是主界面".

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".

这会触发应用程序在启动时加载该界面.这就是在这些情况下创建 self.window 的原因.

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.

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

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天全站免登陆