对于多数 iOS 开发者而言还是更喜欢纯代码开发。然而我们每次创建一个新的项目的时候,项目工程中都会自带一个 storyboard。要去除这个默认 storyboard 进行纯代码开发也并非难事,只要做以下几个步骤。
1.删除项目设置中的 Main interface
1.png2.删除目录文件中的 Main.storyboard
2.png3.在AppDelegate 中添加项目的 window 并添加所需的 viewController
[Objective-C]
//记得要引入对应的 viewController 头文件哟!
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
self.window= [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// show root controller.
self.window.rootViewController= [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
[Swift]
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
return true
}
4.运行APP
运行app 之前最好能先清理生成文件夹,不让可能会出现运行失败的情况。清理生成文件夹的快捷键是 Shift+Command+Alt+K.
网友评论