UIWindow

UIWindow是显示控件的基础,系统在加载程序时次序如下:

  1.创建UIWindow,
  2.创建控制器,将控制器设置为UIWindow的跟控制器,
  3.加载控制器的view,
  4.将view上的控件渲染上去,显示出来.
  


 在执行以下代码以前,请进行以下设置:
UIWindow_第1张图片

下面介绍下加载细节:

程序启动时,首先加载main.m,执行以下函数:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

该函数会执行以下步骤:

1.根据principalClassName提供类名创建UIApplication对象

2.创建UIApplicationDelegate对象,并且成为UIApplication对象的代理

3.开启主运行循环.保持程序一直在运行

4.加载info.plist,判断有没有指定main.storyboard,指定了就加载

如果加载storyboard做的事

1.创建窗口

2.加载main.storyboard,并且加载main.storyboard指定的控制器

3.把新建的控制器作为窗口的跟控制器,让窗口显示出来.

*如果没有指定main.storyboard,系统会到APPDelegate.m文件中执行以下方法:

  //程序完成加载时调用,在以下方法中自定义window即可
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//     Override point for customization after application launch.
    return YES;
}

自定义UIWindow


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//     Override point for customization after application launch.
    //创建window属性,并将其赋值给实力属性self.window,否则会在执行完毕后释放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //设置window的背景色
    self.window.backgroundColor = [UIColor yellowColor];
    //创建控制器
    UIViewController *vc = [[UIViewController alloc] init];
    //设置控制器view的背景色
    vc.view.backgroundColor = [UIColor whiteColor];
    //将控制器指定为window的跟控制器
    self.window.rootViewController = vc;
    //将window显示出来
    [self.window makeKeyAndVisible];
    
    return YES;
}

自定义UIWindow(加载storyboard)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//     Override point for customization after application launch.
    //创建window属性,并将其赋值给实力属性self.window,否则会在执行完毕后释放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //设置window的背景色
    self.window.backgroundColor = [UIColor yellowColor];
    //创建storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //从storyboard加载
    ViewController *vc = [storyboard instantiateInitialViewController];
    //设置控制器view的背景色
    vc.view.backgroundColor = [UIColor whiteColor];
    //将控制器指定为window的跟控制器
    self.window.rootViewController = vc;
    //将window显示出来
    [self.window makeKeyAndVisible];
    
    return YES;
}

自定义UIWindow(加载xib)


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //创建window属性,并将其赋值给实力属性self.window,否则会在执行完毕后释放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //从xib加载
    OneViewController *vc = [[OneViewController alloc] initWithNibName:nil bundle:nil];
    //将控制器指定为window的跟控制器
    self.window.rootViewController = vc;
    //将window显示出来
    [self.window makeKeyAndVisible];
    
    return YES;
}

注意点:

在使用自定义xib加载时,initWithNibName:后面可以填nil,也可以填xib的名称.
如果填xib的名称,系统会自动搜索匹配的xib.
如果填nil时,系统会自动搜索同名的xib文件,但优先加载不带Controller结尾的(如:OneView.xib),
如果找不到,会再查找xib以控制器名称命名的文件(如:OneViewController.xib),如果再找不到,系统会自动创建空的对象.

官方文档描述如下:

This property contains the value specified at initialization time to the >initWithNibName:bundle: method. The value of this property may be nil.

If you use a nib file to store your view controller'��s view, it is recommended that you
specify that nib file explicitly when initializing your view controller. However, if you >do not specify a nib name, and do not override the loadView method in your custom >subclass, the view controller searches for a nib file using other means. Specifically, it >looks for a nib file with an appropriate name (without the .nib extension) and loads that >nib file whenever its view is requested. Specifically, it looks (in order) for a nib file >with one of the following names:

If the view controller class name ends with the word ‘Controller’, as in MyViewController, >it looks for a nib file whose name matches the class name without the word ‘��Controller’, >as in MyView.nib.

It looks for a nib file whose name matches the name of the view controller class. For >?>example, if the class name is MyViewController, it looks for a MyViewController.nib file.

NOTE
Nib names that include a platform-specific identifier such as ~iphone or ~ipad are loaded >only on a device of the corresponding type. For example, a nib name of >MyViewController~ipad.nib is loaded only on iPad. If your app supports both platform >types, you must provide versions of your nib files for each platform.

你可能感兴趣的:(UIWindow)