UIWindow是顯示控件的基礎,系統在加載程序時次序如下:
1.創建UIWindow,
2.創建控制器,將控制器設置為UIWindow的跟控制器,
3.加載控制器的view,
4.將view上的控件渲染上去,顯示出來.
在執行以下代碼以前,請進行以下設置:
下面介紹下加載細節:
程序啟動時,首先加載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.