利用segment切換三個(gè)控制器

需求如下:


屏幕快照 2019-04-18 上午9.45.45.png

點(diǎn)擊今日待辦、我的待辦、我的辦結(jié)切換不同的頁面。

思路:

  • 上一篇文檔中,我針對segment切換頁面的時(shí)候,采用的是數(shù)據(jù)源的切換,然后頁面共用的是一個(gè)。但是今天這個(gè)需求變了,因?yàn)槊總€(gè)頁面對應(yīng)的數(shù)據(jù)源是有些變化的,比如今日待辦中,每一個(gè)item中,是有點(diǎn)擊下載的按鈕,包括不同的頁面,對應(yīng)的headerView也不一樣。所以我就想到了,創(chuàng)建一個(gè)管理器VC來管理三個(gè)不同的控制器。每個(gè)控制器完成各自的功能。

具體實(shí)現(xiàn):

@interface SCInspectionManageVC ()
@property (nonatomic,strong)SCSegmentView *segmentView;
@property (nonatomic, strong)SCInspectionTodayTodoVC *todayVC;//今日待辦
@property (nonatomic, strong)SCInspectionTodoVC *todoVC;     //我的待辦
@property (nonatomic, strong)SCInspectionCompleteTodoVC *completeVC;//我的辦結(jié)
@property (nonatomic, strong) UIViewController *currentViewController;
@property (nonatomic,strong)SCInspectionTodoInteractor *interactor;

@end
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavItem];
    [self addSubViews];
    [self setConstraints];
    [self addChildViewControllers];
}

- (void)addChildViewControllers{
    self.todayVC = [[SCInspectionTodayTodoVC alloc] init];
    self.todayVC.view.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, kSCREEN_WIDTH, kSCREEN_HEIGHT-iPhoneXSafeAreaBottomHeight-kStatusBarAndNavigationBarHeight);
    [self addChildViewController:self.todayVC];
    
    self.todoVC = [[SCInspectionTodoVC alloc] init];
    self.todoVC.view.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, kSCREEN_WIDTH, kSCREEN_HEIGHT-iPhoneXSafeAreaBottomHeight-kStatusBarAndNavigationBarHeight);
    [self addChildViewController:self.todoVC];
    
    self.completeVC = [[SCInspectionCompleteTodoVC alloc] init];
    self.completeVC.view.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, kSCREEN_WIDTH, kSCREEN_HEIGHT-iPhoneXSafeAreaBottomHeight-kStatusBarAndNavigationBarHeight);
    [self addChildViewController:self.completeVC];
    
//設(shè)置當(dāng)前最先展示的頁面 很重要
    self.currentViewController = self.todayVC;
    [self.view addSubview:self.todayVC.view];
}

- (void)setConstraints{
    [self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.top.leading.trailing.mas_equalTo(self.view);
    }];
}

- (void)setNavItem{
    self.title = @"巡檢待辦";
}

-(void)addSubViews{
    [self.view addSubview:self.segmentView];
    self.view.backgroundColor = [UIColor SCBackGroundColor];
}

-(SCSegmentView *)segmentView{
    if (!_segmentView) {
        _segmentView = [[SCSegmentView alloc]initWithItemList:@[@"今日待辦",@"我的待辦",@"我的辦結(jié)"]];
        @Weakify(self);
        _segmentView.segmentSwitchBlock = ^(long segmentIndex) {
            @Strongify(self);
            [self segmentValueChanged:segmentIndex];
        };
    }
    return _segmentView;
}

-(SCInspectionTodoInteractor *)interactor{
    return _interactor = _interactor?:[[SCInspectionTodoInteractor alloc]init];
}

下面這個(gè)是實(shí)現(xiàn)標(biāo)簽切換最重要的一個(gè)步驟

- (void)segmentValueChanged:(long )selectedSegmentIndex{
    if ((self.currentViewController==self.todayVC&&selectedSegmentIndex==0)||(self.currentViewController==self.todoVC&&selectedSegmentIndex==1) ||(self.currentViewController==self.completeVC&&selectedSegmentIndex==2) ) {
        return;
    }
    
    UIViewController *oldViewController=self.currentViewController;
    switch (selectedSegmentIndex) {
        case 0:
            [self replaceFromOldViewController:oldViewController toNewViewController:self.todayVC];
            break;
        case 1:
            [self replaceFromOldViewController:oldViewController toNewViewController:self.todoVC];
            break;
        case 2:
            [self replaceFromOldViewController:oldViewController toNewViewController:self.completeVC];
            break;

        default:
            break;
    }
}

//控制器切換
- (void)replaceFromOldViewController:(UIViewController *)oldVC toNewViewController:(UIViewController *)newVC{
    [self transitionFromViewController:oldVC
                      toViewController:newVC
                              duration:1
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                            }
                            completion:^(BOOL finished) {
                                if (finished) {
                                    self.currentViewController=newVC;
                                }else{
                                    self.currentViewController=oldVC;
                                }
                            }];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • “忙,懶,煩” 似乎成了很多人不愿提早10分鐘起來做早餐的理由。 對于早餐,很多人都選要么不吃,要么隨便吃。忙著上...
    小芳帶你健康瘦閱讀 716評論 1 0
  • 說實(shí)話,處在這樣一個(gè)信息爆炸的年代,要讓自己知道對自己而言什么事是真正重要的,挺難。先不說別的,就拿“吃什么健康?...
    yydhdl閱讀 788評論 0 1
  • 上海女人 下午,去街道辦事處辦事。進(jìn)門,感覺像走進(jìn)一家銀行,寬敞,潔凈。人不多,也不...
    東來到西啦閱讀 217評論 0 1
  • 最近特別困,坐著就能睡著,不分時(shí)間不分場合。俗話說“春困秋乏夏打盹,睡不醒的冬三月”,看來天冷的真是提前入冬了。明...
    沈磊閱讀 354評論 0 0

友情鏈接更多精彩內(nèi)容