大家都叫我胖子,这是我第一次写博客,若果有人看见以及发现我写的不好请多多批评指正。话不多说直接开始。

       在开始做ios的时候特别喜欢用storyboard觉得这个东西非常方便好用,但是在一次一个项目写完后需要离职时出现了问题,问题是新来的程序猿完全看不懂我的代码,说没有办法在我写的项目上修改。我开始觉得是他的问题直到我接到一个项目后我发现我错了,我接到的项目和我原来交接的项目一样全是用storyboard拖拽的内容storyboard中有成千上万的viewController和数不清的线条,我发现我的查看项目内容速度非常缓慢,最后我觉得还不如自己开发一个他的以前的内容。于是我又开始了纯代码搭建项目;但是后来我又尝试用storyboard搭建项目框架,发现有很多可取之处。下面分享给大家,供大家评价。

1.项目的框架搭建(纯代码):

(1)首先我们上来打开xcode选择新建项目。接下来我们需要删除掉系统帮助我们生成的ViewController(带storyboard),然后在自己创建RootViewController(不带storyboard),那么我们就需要在项目的AppDelegate.m文件中告诉系统我们创建的控制器。

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    RootViewController *root = [[RootViewController alloc] init];
    [self.window setRootViewController:root];
    NewFeatureViewController *newFeature = [[NewFeatureViewController alloc] init];
    [self.window setRootViewController:newFeature];

(2)接下来我们就可以在rootViewcontroller这个类中搭建我们需要的项目主框架了,我们的框架主要有2部分构成,第一部分就是我们的dock栏第二部分是我们的控制器。我们先来说第一部分。需要注意的是我们的控制器一定要在dock栏创建完成之前创建,由于我们会默认点击第一个按钮app开始运行要让视图停留在主页中,如果我们点击了按钮发现没有创建的视图,程序会崩溃。

- (void)viewDidLoad {
    [super viewDidLoad];
    
//  添加试图控制器
    [self addSubControllers];
    
//  添加底部四个按钮点击试图
    [self addDaockView];
    
//  添加分割线试图
    [self addCarveView];
}
#pragma mark 添加底部四个按钮点击试图
- (void)addDaockView{
    
    DockView *dockView = [[DockView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - kDockHeight, self.view.bounds.size.width, kDockHeight)];
    [dockView setDelegate:self];
    
    [dockView addDockViewItemWithDockName:@"首页" andNormalName:@"operation_normal" andSelectedName:@"operation_selected"];
    [dockView addDockViewItemWithDockName:@"圈子" andNormalName:@"circle_normal" andSelectedName:@"circle_selected"];
    
    [dockView addDockViewItemWithDockName:@"订单" andNormalName:@"order_normal" andSelectedName:@"order_selected"];
    [dockView addDockViewItemWithDockName:@"我的" andNormalName:@"mine_normal" andSelectedName:@"mine_selected"];
    
    [self.view addSubview:dockView];
}
#pragma mark 添加试图控制器
- (void)addSubControllers{
    
//  首页
    OperationViewController *operattion = [[OperationViewController alloc] initWithNibName:@"OperationViewController" bundle:nil];
    MyNavigationViewController *operationNavigation =[[MyNavigationViewController alloc] initWithRootViewController:operattion];
    [self addChildViewController:operationNavigation];
    
//  圈子
    CircleViewController *circle = [[CircleViewController alloc] initWithNibName:@"CircleViewController" bundle:nil];
    MyNavigationViewController *circleNavigation = [[MyNavigationViewController alloc] initWithRootViewController:circle];
    [self addChildViewController:circleNavigation];
    
//  订单
    OrderViewController *order = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil];
    MyNavigationViewController *orderNavigation = [[MyNavigationViewController alloc] initWithRootViewController:order];
    [self addChildViewController:orderNavigation];
    
//  我的
    MineViewController *mine = [[MineViewController alloc] initWithNibName:@"MineViewController" bundle:nil];
    MyNavigationViewController *mineNavigation = [[MyNavigationViewController alloc] initWithRootViewController:mine];
    [self addChildViewController:mineNavigation];
}


在dockView的文件中我们需要传入图标的名称以及图片dockView.h

#import <UIKit/UIKit.h>

@class DockView;

@protocol DockViewDelegate <NSObject>

@optional
//  按钮点击从那个按钮点击到那个按钮
- (void)dockViewWithDockItem:(DockView *)dockView andFromTag:(NSInteger)fromTag andToTag:(NSInteger)toTag;

@end

@interface DockView : UIView

//  代理属性
@property (nonatomic,strong) id <DockViewDelegate> delegate;

#pragma mark 动态添加DockItem方法
- (void)addDockViewItemWithDockName:(NSString *)dockName andNormalName:(NSString *)normalName andSelectedName:(NSString *)selectedName;


@end


dockView.m

#import "DockView.h"
#import "DockItem.h"

@interface DockView ()

@property (nonatomic,strong) DockItem *recordDockItem;

@end

@implementation DockView

#pragma mark 动态添加DockItem方法
- (void)addDockViewItemWithDockName:(NSString *)dockName andNormalName:(NSString *)normalName andSelectedName:(NSString *)selectedName{
    
    DockItem *dockItem = [[DockItem alloc] initWithFrame:CGRectZero andDockName:dockName  andNormalName:normalName andSelectedName:selectedName];
    [dockItem addTarget:self action:@selector(dockItemCilck:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:dockItem];

    NSInteger itemCount =self.subviews.count;
    
    CGFloat itemWidth = self.bounds.size.width / itemCount;
    
    if (itemCount == 1) {
        
        [self dockItemCilck:dockItem];
    }
    
    for (NSInteger i = 0; i < itemCount; i ++) {
        
        DockItem *item = self.subviews[i];
        
        [item setTag:i];

        [item setFrame:CGRectMake(i * itemWidth, 0, itemWidth, kDockHeight)];
    }
}

#pragma mark dockItem的点击事件
- (void)dockItemCilck:(DockItem *)sender{
    
    [self.recordDockItem setSelected:NO];
    
    [sender setSelected:YES];
    
    if ([self.delegate respondsToSelector:@selector(dockViewWithDockItem:andFromTag:andToTag:)]) {
        
        [self.delegate dockViewWithDockItem:self andFromTag:self.recordDockItem.tag andToTag:sender.tag];
    }
    
    self.recordDockItem = sender;
}

@end

这样写的好处是我们可以动态的添加需要的item内容也可以随时删除任何item内容不会像storyboard那样出现删除了文件没有删除关联系统奔溃的情况,最好的是可以在几分钟把代码剥离出来用在其他的项目中,运行的效果是:


ios项目安装命令是什么 ios项目搭建_ios项目安装命令是什么

         

ios项目安装命令是什么 ios项目搭建_ci_02


ios项目安装命令是什么 ios项目搭建_ios项目安装命令是什么_03

         

ios项目安装命令是什么 ios项目搭建_ios_04


2.项目的框架搭建(storyboard拖拽):