天天看點

iOS建立視圖控制器的方法總結

1.xib建立控制器,先建立自定義控制器類,然後指定xib檔案,修改xib檔案中的File's Owner的custom class,設定為你自定義的控制器類.

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; XibViewController * xibVc=[[XibViewController alloc]initWithNibName:@"ZXib" bundle:nil]; self.window.rootViewController=xibVc; [self.window makeKeyAndVisible];

2.純代碼建立根視圖控制器,在Appdelegate中的didFinishLaunchingWithOptions

self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; //設定視窗根視窗控制器 self.window.rootViewController //将視窗作為主視窗被設定可見 [self.window makeKeyWindow];

3.通過storyboard檔案來建立,加載storyboard中的控制器就可以了。從這個檔案中執行個體化控制器。當你自己建立了個storyboard可以這樣進行設定,但是你要保證你的這個storyboard檔案不為空,以及設定了Is Initial View Controller.如果我們是用系統自帶的就不要去修改了還是直接用就可以了。

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; //加載檔案 UIStoryboard * board=[UIStoryboard storyboardWithName:@"ZXStoryboard" bundle:nil];//nil就代表在mainBundle這個路徑下 //從storyboard中執行個體化控制器 UIViewController * viewController=[board instantiateInitialViewController]; self.window.rootViewController=viewController; [self.window makeKeyAndVisible];

iOS