iOS日常须知

ios 架构思路(一)

2018-06-06  本文已影响121人  阿汤8阿义

在网上有很多架构一类的文章,每篇文章都有自己的特色。但他们有个共同的特点,他们的基础架构都是MVC。网上的架构文章这么多但不定有一个很完全架构文章是适合我们自己开发的APP。那边我这里就不进行讲某个具体的架构,我这里只是我为什么要这样去做。

搭建UI结构

我这里的思路都是依据纯代码进行的。
这里围绕的思路就是UI,我们如何操作才能控制性能、减少代码量、提高复用性同时避免耦合性。

代码:
在Controller上add平台UIView

- (void)initWithInstanceView {
    TYInstancePlatformView *view = [TYInstancePlatformView addInstanceDataModel:_dataModel];
    [self.view addSubview:view];
}

在平台上添加布局UIView和在此UIView上进行布局。

//添加布局UIView
- (void)addWithInstanceDataModel:(TYInstanceDataModel *)dataModel {
    TYInstanceLayoutView *view = [TYInstanceLayoutView addInstanceLayoutView:dataModel];
    [self addSubview:view];
}

在此UIView上进行布局

- (void)initViewDataModel:(TYInstanceDataModel *)dataModel {
    UIImageView *imageView = [UIImageView new];
    imageView.left = 0;
    imageView.top = 0;
    imageView.width = w;
    imageView.height = 400;
    NSString *filePath=[[NSBundle mainBundle] pathForResource:dataModel.image ofType:@"png"];
    imageView.image = [UIImage imageNamed:filePath];
    [self addSubview:imageView];
    
    UILabel *lbl = [UILabel new];
    lbl.left = [dataModel.sizes[@"left"] floatValue];
    lbl.top = imageView.top + imageView.height + 10;
    lbl.width = [dataModel.sizes[@"width"] floatValue];
    lbl.height = [dataModel.sizes[@"height"] floatValue];
    lbl.numberOfLines = 0;
    lbl.text = dataModel.textData;
    lbl.font = [UIFont systemFontOfSize:14];
    [self addSubview:lbl];
    
    UITextField *textField = [UITextField new];
    textField.left = 0;
    textField.top = lbl.top + lbl.height + 10;
    textField.width = 200;
    textField.height = 50;
    [self addSubview:textField];
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.left = textField.left + textField.width;
    but.top = lbl.top + lbl.height + 10;
    but.width = 100;
    but.height = 50;
    [but setTitle:@"按钮" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(selectorBut) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:but];
}

添加数据页面逻辑处理data
这里要考虑的是,在那块来创建数据处理器。是在UI平台上还是在view上来创建数据数据处理期。
第一种:
在UI平台上,view(页面)和data(数据处理)是没有交际的,他们靠的是平台来建立两者之间的关系。优点是,他们两者之间分离,没有耦合性。在其它地方的复用性也高。缺点也正是它缺点带来的,要使用代理和block来建立两者的联系。
第二种:在view(页面)中创建,这样两者就存在联系。但问题是耦合性非常高,没有什么复用性。
在这里我使用了第一种形式来创建。
代码:
在平台创建两者

//view为TYInstanceLayoutView,data为TYInstancaProcessData
- (void)addWithInstanceDataModel:(TYInstanceDataModel *)dataModel {
    TYInstanceLayoutView *view = [TYInstanceLayoutView addInstanceLayoutView:dataModel];
    [view initViewDataModel:dataModel dealBlock:^(int deal) {
        BOOL boo = [TYInstancaProcessData incomingWithInt:deal];
        [view whetherHideWithTextField:boo];
    }];
    [self addSubview:view];
}

在data处理数据

+ (BOOL)incomingWithInt:(int)data {
    if (data == 1) {
        return YES;
    }else {
        return NO;
    }
}

在view中显示处理完的数据

- (void)whetherHideWithTextField:(BOOL)boo {
    _textField.hidden = boo;
}

这里我通过一个相对较为简单的代码来进行架构的的说明,这里只是一个简单的对UI搭建和逻辑处理的架构解释。这个也算是我们APP最基础的模块。

上一篇 下一篇

猜你喜欢

热点阅读