Xcode5中如何切換Storyboards爲xib

在Xcode5中,當建立一個帶View的iPhone項目時,默認必須使用Storyboards,再也不支持切換xib的checkbox。本文講解如何手動切換到使用xib來佈局。html

1,把Main.storyboard從項目中移除xcode

 

2,添加xib文件到項目中。添加一個新文件,選擇View,命名和*ViewContorller相同。app

 

3,把Main storyboard對應的項從plist文件中移除ide

 

4,在*AppDelegate中添加相似代碼佈局

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;

     // Override point for customization after application launch.

     TestViewController *test = [[TestViewController alloc]     initWithNibName:@"TestViewController" bundle:nil];
     UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:test];
     self.window.rootViewController = nav;

     [self.window makeKeyAndVisible];

     return YES;
}

若是ARC關閉的話,以上代碼須要手動添加autorelease,這裏不詳述了。ui

 

5,可選:關閉ARC。 在項目的build setting中,找到 Objective-C Automatic Reference Counting, 設置爲Nospa

image

 

6,把xib關聯到對應的ViewController上,不然會報以下錯誤設計

NibName[2203:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "loc" nib but the view outlet was not set.'3d

 

步驟(參考自 loaded some nib but the view outlet was not set):code

1). 點擊咱們要加載的 xib 文件

2). 在右邊選中 File's Owner

image

3). 在 File's Owner 的 image 選項卡的「Custom Class」 屬性設置中,將 Class 的值改爲對應的 VC, 這裏改爲 UIViewController,

image

image

4). 這時候,在File's Owner 的  image選項卡中, 就 會出現「待鏈接設置」 的 view 屬性, 也即咱們的編譯器 告訴咱們的 the view outlet was not set  中的 view。當 File's Owner 的 class 爲 NSObject 時候,是沒有 view 屬性的。

image

鏈接 view 屬性(把連線拖動到xib設計器中進行鏈接),

image

 

參考:

http://www.cnblogs.com/TivonStone/archive/2012/04/20/2460116.html

http://stackoverflow.com/questions/17234172/xcode-5-without-storyboard-and-arc

相關文章
相關標籤/搜索