需求如下:
屏幕快照 2019-04-18 上午9.45.45.png
點(diǎn)擊今日待辦、我的待辦频鉴、我的辦結(jié)切換不同的頁面栓辜。
思路:
- 上一篇文檔中,我針對(duì)segment切換頁面的時(shí)候垛孔,采用的是數(shù)據(jù)源的切換藕甩,然后頁面共用的是一個(gè)。但是今天這個(gè)需求變了周荐,因?yàn)槊總€(gè)頁面對(duì)應(yīng)的數(shù)據(jù)源是有些變化的狭莱,比如今日待辦中,每一個(gè)item中概作,是有點(diǎn)擊下載的按鈕腋妙,包括不同的頁面,對(duì)應(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;
}
}];
}