在新项目中弃用StoryBoard

由于各种各样的原因(尤其是多人协作,避免代码/样式冲突显得尤为重要。),可能你需要使用传统的Nib方式布局iOS样式。而新版的Xcode是默认使用Storyboard的,所以我们需要改变这种默认行为。
禁用Storyboard的流程如下:

在设置中删除Main Interface

设置Main Interface

  • 在导航试图中选中项目
  • 在Targets选择你的target
  • 在Deployment Info section(节)找到
  • 删除main interface中的Main(1,全选Main;2,delete;3,tab)
    :虽然程序会自动查找Main.Storyboard,但是我们并不需要删除Main.Storyboard。当然,如果你是处女座,请自便。

在AppDelegate中设置默认启动的ViewController

  • 在AppDelegate头文件中引入你需要作为rootController的ViewController

    /**

    • 引入一个ViewController,
    • 这里设置的是ViewController,
    • 你也可以设置成为其他的
      */

    #import “ViewController.h”`

  • 在AppDelegate头文件中加入ViewController属性

    `/**
    * 定义ViewController属性
    */
    @property (strong, nonatomic) ViewController *viewController;`
  • 在实现文件AppDelegate中引入头文件中的viewController

    `/**
    * 在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加以下代码
    */
    // Use Nib instead of StoryBoard
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

设置Nib文件

  • 新建一个与ViewController同名的xib文件(新建文件的时候选择View)

  • 设置xib文件的File’s Owner。将其Custom class设置成ViewController
    设置File

  • 设置File’s owner的Outlets。在Outlet面板的Outlets节点,把view映射到xib中
    设置Outlets View

像StoryBoard一样处理xib

接下来,你可以在xib中随心所欲的操作view啦。把它当一个StoryBoard来处理就好。