一絮姆、UIWindow的作用呀酸?
- A UIScreen object that identifies physical screen connected to the device.
- A UIWindow object that provides drawing support for the screen.
- A set of UIView objects to perform the drawing. These objects are attached to the window and draw their contents when the window asks them to.(不太好翻譯)
二、系統(tǒng)如何自動創(chuàng)建Window和window的根控制器?
- 在main函數(shù)中,UIApplicationMain會加在info.plist文件碰辅,并判斷有沒有指定Main Interface, 系統(tǒng)默認的Main Interface指向Main.storyboard,然后自動創(chuàng)建window介时,并加在Main.storyboard中的啟動控制器(initial View Controller)没宾。
三、如何用代碼方式創(chuàng)建window和window的根控制器沸柔?
1.首先刪除Main Interface中的內(nèi)容
2.AppDelegate中代碼創(chuàng)建如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *VC = [[UIViewController alloc] init];
// VC.view.backgroundColor = [UIColor orangeColor];
self.window.rootViewController = VC;
[self.window makeKeyAndVisible];
return YES;
}
四循衰、如何從自定義的storyboard中創(chuàng)建window的根控制器
1.創(chuàng)建一個storyboard, 命名為"XJStoryboard"
2.storyboard中拖一個viewController進去,并設(shè)置為這個storyboard的initial viewController
3.AppDelegate中代碼創(chuàng)建如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"XJStoryboard" bundle:nil];
self.window.rootViewController = storyboard.instantiateInitialViewController;
[self.window makeKeyAndVisible];
return YES;
}
- 注意:上面的代碼其實不寫也可以褐澎,直接在Main Interface 中指定你需要加載的storyboard会钝,系統(tǒng)會自動創(chuàng)建window和根控制器。
五乱凿、如何從自定義的xib中創(chuàng)建window的根控制器
1.xib的創(chuàng)建
2.上面創(chuàng)建的是一個空的xib顽素,現(xiàn)在拖一個UIView進去咽弦,設(shè)置這個xib的File's Owner為ViewController徒蟆,并把ViewController的View指向剛剛拖進區(qū)的UIView,如下圖所示,
3.代碼部分
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 這里的ViewController借用了工程創(chuàng)建時型型,Xcode自動創(chuàng)建的類
// 這里完全可以直接創(chuàng)建一個自定義的帶xib的Controller
// 這里主要展示如何自己創(chuàng)建一個空的xib段审,然后關(guān)聯(lián)已存在的類
UIViewController *VC = [[ViewController alloc] initWithNibName:@"XJXib" bundle:nil];
self.window.rootViewController = VC;
[self.window makeKeyAndVisible];
return YES;
}