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界面 請自行創建 組合控件,如果有重複的地方可以 封裝。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章