IOS-MVC设计模式深入

大家都知道我们开发采用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 && [_delegate respondsToSelector:@selector(showBrandProduct:)]) {

        [_delegate showBrandProduct:dict];

    }

}

- (void)showGroup:(NSDictionary *)dict

{

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

        [_delegate showGroup:dict];

    }

}


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

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

- (void)showBrandProduct:(NSDictionary *)dict

{

    if (!isProductMode) {

        isProductMode = !isProductMode;

        _tableViewVC.view.hidden = YES;

        [self transitionFromViewController:currentVC toViewController:_collectionVC duration:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            

        } completion:^(BOOL finished) {

            _collectionVC.view.hidden = NO;

            currentVC = _collectionVC;

            

        }];

    }

}

- (void)showGroup:(NSDictionary *)dict

{

    if (isProductMode) {

        isProductMode = !isProductMode;

        _collectionVC.view.hidden = YES;

        [self transitionFromViewController:currentVC toViewController:_tableViewVC duration:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            

        } completion:^(BOOL finished) {

            _tableViewVC.view.hidden = NO;

            currentVC = _tableViewVC;

        }];

    }

}



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值