iOS相关

纯代码搭建iOS工程

2016-03-26  本文已影响586人  ppsheep

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

从我开始接触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都能够清晰展示出来

上一篇 下一篇

猜你喜欢

热点阅读