利用segment切换三个控制器
2019-04-18 本文已影响1人
羊妞麻麻
需求如下:
屏幕快照 2019-04-18 上午9.45.45.png
点击今日待办、我的待办、我的办结切换不同的页面。
思路:
- 上一篇文档中,我针对segment切换页面的时候,采用的是数据源的切换,然后页面共用的是一个。但是今天这个需求变了,因为每个页面对应的数据源是有些变化的,比如今日待办中,每一个item中,是有点击下载的按钮,包括不同的页面,对应的headerView也不一样。所以我就想到了,创建一个管理器VC来管理三个不同的控制器。每个控制器完成各自的功能。
具体实现:
@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;
}
}];
}