纯代码搭建iOS工程

扯一下自己对纯代码的看法

从我开始接触iOS开发以来,就是一直采用的纯代码手写,俗称肉码。这个当时也是因为项目组里面当时的组长的要求,说是这样便于大家的协同开发,不过后面确实也印证了这一说法,纯代码开发确实对像我一样的菜鸟来说,更容易看懂一些。
虽然现在,我也还在坚持纯代码编写工程,但是确实也看到的一些不妥,效率相对较低,而且Apple出了Interface Builder 工具,为什么不尝试呢,所以今后也会向IB开进,学习。

Xcode创建一个single project

Snip20160326_12.png

然后进入工程,将main.stroreboard删除掉

在工程配置文件里 将Main Interface清空,这里需要注意 删除掉后,需要保存 最好运行一次,不然会删除失效的

Snip20160326_14.png

然后找到Applegate.m在 didFinishLaunchingWithOptions 方法写程序入口 一般我的习惯是创建一个main tab viewController 用来管理几个tab 当然这也是看需求的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    YQPPMainViewController *vc = [[YQPPMainViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

这样程序的入口就变成了YQPPMainViewController这个viewcontroler 然后可以在这个tabcontroller里面加入一些子controller等

- (void)viewDidLoad {
    [super viewDidLoad];
    _firstVC = [[YQPPFirstViewController alloc] init];
    [self addChidVC:_firstVC title:@"首页" image:[UIImage imageNamed:@"first_normal"] selectImage:[UIImage imageNamed:@"first_select"]];
    _secondVC = [[YQPPSecondViewController alloc] init];
    [self addChidVC:_secondVC title:@"二页" image:[UIImage imageNamed:@"second_normal"] selectImage:[UIImage imageNamed:@"second_select"]];
    _thirdVC = [[YQPPThirdViewController alloc] init];
    [self addChidVC:_thirdVC title:@"三页" image:[UIImage imageNamed:@"third_normal"] selectImage:[UIImage imageNamed:@"third_select"]];
    _fourthVC = [[YQPPFourthViewController alloc] init];
    [self addChidVC: _fourthVC title: @"四页" image:[UIImage imageNamed:@"fourth_normal"] selectImage: [UIImage imageNamed:@"fourth_select"]];
}
/**
 *  添加子控制器
 *
 *  @param viewController 控制器
 *  @param title          tabbar的title
 *  @param image          未选中的图片
 *  @param selectImage    选中的图片
 */
- (void)addChidVC:(UIViewController *)viewController title:(NSString *)title image:(UIImage *)image selectImage:(UIImage *)selectImage {
    
    //设置图标和标题
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [viewController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    //包装一个navigationvc
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nvc];
}

每一个tabbar对应的controller都能够清晰展示出来

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,907评论 25 709
  • 端午上班,没来得及回家。当听三姐说父母在家一天没有吃饭,我震惊了。什么时候,年迈的父母变得这么任性和倔强?每每说起...
    贾学礼零售阅读 1,171评论 0 0
  • 还记得《霍乱时期的爱情》里第一章,是以一个摄影师阿莫乌尔的自杀引出故事中的人物的,刚看的时候我就被阿莫乌尔的做...
    荏苒几盈虚阅读 1,643评论 0 1
  • 可能真的是名利害人吧!若是一个人普普通通、平平凡凡度过一生,也许他可以谨守着世间的基本礼仪。若是他的天资再醇厚忠良...
    化浊阅读 3,880评论 2 4
  • 我平时不大有机会下厨做饭,家里有老爸在打理全家的饮食,相当于他退休后的事业,所以我美其名曰全力支持,实际上是为自己...
    家中的阿雅阅读 1,486评论 0 0

友情链接更多精彩内容