2013年10月27日 星期日

Xcode 5 製作xib檔案

身為一個IOS新手 看看書學程式沒什麼了不起的
但Xcode更新成5之後
靠北 內建直接就是使用Storyboard
可是台灣市面上的書都是從原始的xib檔案開始教 甚至很多書都建議不要使用Storyboard
那怎麼辦才好勒 囧

孤狗了一下發現有人有和我一樣的問題


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

http://stackoverflow.com/questions/19092605/there-is-no-xib-file-when-i-create-a-new-single-view-application-in-xcode-5

大致上是
新增一個IOS的空專案
接著新增一個Objective-C Class
繼承UIViewController 並且把With XIB for user interface打勾

若要當rootViewController有兩種做法

一是不強制綁AppDelegate
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    ViewController* viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = nav;    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

另一做法是綁在AppDelegate
AppDelegate.h:
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate]]> 
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController* viewController; @end

AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

差異在於不需要再創建UINavigationController設定rootViewController後再指定去UIWindow的rootViewController

沒有留言:

張貼留言