程序员

利用segment切换三个控制器

2019-04-18  本文已影响1人  羊妞麻麻

需求如下:


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

点击今日待办、我的待办、我的办结切换不同的页面。

思路:

具体实现:

@interface SCInspectionManageVC ()
@property (nonatomic,strong)SCSegmentView *segmentView;
@property (nonatomic, strong)SCInspectionTodayTodoVC *todayVC;//今日待办
@property (nonatomic, strong)SCInspectionTodoVC *todoVC;     //我的待办
@property (nonatomic, strong)SCInspectionCompleteTodoVC *completeVC;//我的办结
@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];
    
//设置当前最先展示的页面 很重要
    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:@[@"今日待办",@"我的待办",@"我的办结"]];
        @Weakify(self);
        _segmentView.segmentSwitchBlock = ^(long segmentIndex) {
            @Strongify(self);
            [self segmentValueChanged:segmentIndex];
        };
    }
    return _segmentView;
}

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

下面这个是实现标签切换最重要的一个步骤

- (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;
                                }
                            }];
}
上一篇下一篇

猜你喜欢

热点阅读