Xcode5中如何切換Storyboards爲xib

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

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

 

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

 

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

 

4,在*AppDelegate中添加類似代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (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,這裏不詳述了。

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

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.'

 

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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章