控制器viewController的三種創建方式
在didFinishLaunchingWithOptions中創建並添加控制器:
1.alloc】init】
2.storyboard
3.xib
1.alloc init
創建控制器
2.storyboard
刪除main.storyboard,刪除info.plist中Launch screen interface file base name文件。否則會一直報錯could not find a storyboard named ‘Main’ in bundle NSBundle.
新建一個自定義storyboard,名稱test,代碼中創建一個storyboard加載自自定義的storyboard類,爲storyboard設置初始控制器,若有多個控制器可以用設置storyboardID進行區分。將加載自自定義storyboard中的控制器設爲self.window的rootViewController。
(自定義的storyboard中可以不指定初始控制器,此時加載storyboard時需要指明控制器的標示符,否則報錯)
3、xib
新建自定義xib文件,拖入一個view(否則不能加載was unable to load a nib named “***”)添加一個關聯自定義xib的類繼承自UIViewController。創建一個加載自自定義xib的控制器。(需要將xib的view設置爲自定義控制類的view,否則未設置輸出口”loaded the “two” nib but the view outlet was not set.“,設置File’s Owner將其與view連線,而在storyboard中默認已經連了線)。
注意點:
必須先講xibFile’s Owner的class改成類名,才能進行與view的連線操作。若刪除class名此時連線還在還可以正確管理,但連線又無法連接了。