xcode5设置自定义*.xib文件为main interface

从xcode5/iOS SDK 7.0开始,新建Single View Application默认界面是*.storyboard文件

如果删除*.storyboard新建自定义的xib文件,然后在Project Settings里设置的Main Interface为xib文件的话,运行时会报NSNullException错误。

 

用以下方法修改 AppDelegate.h/AppDelegate.m两个文件就可以使用自定义的xib做Main Interface

首先Project Settings -> General -> Deployment Info 里的Main Interface设为空,然后再让Delegate来引导第一个页面

 

  1. 新建RootViewController,包括.m/.h/.xib文件
  2. AppDelegate.h
    // AppDelegate.h
    #import <UIKit/UIKit.h>
    #import "RootViewController.h"
    
    @class RootViewController;
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) RootViewController *rootView;
    
    @end
  3. AppDelegate.m
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.rootView = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
        
        if ([self.window respondsToSelector:@selector(setRootViewController:)]) {
            self.window.rootViewController = self.rootView;
        }
        
        else {
            [self.window addSubview:self.rootView.view];
        }
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    @end
    

    到这里就完成了Main Interface的设置。
    如果需要用NavigationController/TabBarController引导页面的话,用NavigationController/TabBarController的定义替换RootViewController,再用ViewController设置为他们的RootView就可以了

posted @ 2014-07-19 14:27  evsteel  阅读(878)  评论(0)    收藏  举报