IOS-MVC设计模式深入

来源:互联网 发布:云计算能力开放全景图 编辑:程序博客网 时间:2024/05/21 17:36

大家都知道我们开发采用MVC设计模式,但是又有多少人从项目从头到尾一直使用MVC呢。

下面说一下我们如何使用MVC和基础

如果我们有一个要求: 一个页面 有2个按钮 2个按钮 切换不同的数据,第一个按钮是UITableview 第二个是UIScrollview 那么我们应该如何应用在这个页面呢?


2种方法 :

1.我们可以给2个按钮设置不同tag值 进行页面切换 并且切换时隐藏。

//详细说一下第二种方法 必要时 附上代码 .

2.我们创建一个基础VC 用于协调这2个UI控件的VC.

基础VC 只是用于切换视图,这样我们tableView 和scrollview 2个VC 写好 控制器,并且 写好自定义cell 和 scrollview所需要的UI界面。

在2个VC中 创建 UI界面的 实例对象 ,并且执行 方法 我们可以使用代理 代理到基础VC执行 。

#import "MYBrandHeaderView.h"

#import "MYBrandHeaderDelegate.h"


@protocol MYBrandTableViewControllerDelegate <NSObject>


- (void)showBrandProduct:(NSDictionary *)dict;


- (void)showGroup:(NSDictionary *)dict;


@end


@interface MYBrandTableViewController :BaseViewController


@property (nonatomic,strong) UITableView *circleTableView;

@property (nonatomic,strong) MYBrandHeaderView *headerView;


@property (nonatomic,assign)id<MYBrandTableViewControllerDelegate> delegate;


- (instancetype)initWithBrandID:(NSString *)brandID;


//我这里只有一个代理方法 用于显示数据

- (void)showBrandProduct:(NSDictionary *)dict

{

   if (_delegate && [_delegaterespondsToSelector:@selector(showBrandProduct:)]) {

        [_delegateshowBrandProduct:dict];

    }

}

- (void)showGroup:(NSDictionary *)dict

{

    if (_delegate && [_delegaterespondsToSelector:@selector(showGroup:)]) {

        [_delegateshowGroup:dict];

    }

}


//这样 我吧tableview这个页面获取到得dict数据传入 基础VC

//在基础VC中 切换2个页面

- (void)showBrandProduct:(NSDictionary *)dict

{

    if (!isProductMode) {

        isProductMode = !isProductMode;

        _tableViewVC.view.hidden =YES;

        [selftransitionFromViewController:currentVCtoViewController:_collectionVCduration:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{

            

        }completion:^(BOOL finished) {

            _collectionVC.view.hidden =NO;

            currentVC =_collectionVC;

            

        }];

    }

}

- (void)showGroup:(NSDictionary *)dict

{

    if (isProductMode) {

        isProductMode = !isProductMode;

        _collectionVC.view.hidden =YES;

        [selftransitionFromViewController:currentVCtoViewController:_tableViewVCduration:0options:UIViewAnimationOptionCurveEaseInOutanimations:^{

            

        }completion:^(BOOL finished) {

            _tableViewVC.view.hidden =NO;

            currentVC =_tableViewVC;

        }];

    }

}



这样就实现了 MVC 构造。UI界面 请自行创建 组合控件,如果有重复的地方可以 封装。


0 0
原创粉丝点击