[iOS] 使用xib做为应用程序入口 with Code

[iOS] 使用xib做为应用程序入口 with Code

前言

开发iOS APP的时候,使用storyboard能够快速并且直觉的建立用户界面。但在多人团队开发的情景中,因为storyboard是以单一档案的方式存在,很容易造成签出、签入时,档案被锁定、档案合并冲突等等问题的发生。这时开发人员可以选择使用xib做为用户接口的开发单位,将用户接口拆散为独立存在的xib档案,分散团队成员同时编辑同一文件的风险。

在Xcode中默认是以storyboard做为应用程序入口,变更为使用xib做为应用程序入口需要一些额外的步骤。本篇文章说明在iOS APP开发的过程中,如何透过写Code的方式,来使用xib做为应用程序的入口,为自己留个纪录也希望能帮助到有需要的开发人员。

前言01

操作

1. 建立Single View Application

使用Xcode建立新项目,并且选择项目类型为Single View Application。

操作01_01

操作01_02

操作01_03

2. 移除Storyboard

移除Storyboard以及相关档案。

操作02_01

3. 建立ViewController

建立做为入口的MainViewController。(记得要勾选Also create XIB file)

操作03_01

操作03_02

操作03_03

4. 清除Main Interface

清除应用程序的入口设定参数Main Interface

操作04_01

5. 修改AppDelegate

最后在AppDelegate中加入下列程序代码,来启动MainViewController。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
    self.window!.rootViewController = MainViewController(nibName: "MainViewController", bundle: nil)
    self.window!.makeKeyAndVisible()
    return true
}

6. 执行结果

编译并执行范例项目,可以看到iOS APP中,已正确使用xib做为应用程序入口。

操作06_01

参考数据

原文地址:https://www.cnblogs.com/clark159/p/4338733.html