segmentControl实现控制器的切换
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1 创建窗口
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
//2 设置主控制器
XCMainController *mainVc = [[XCMainController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVc];
self.window.rootViewController = nav;
//3 显示window
[self.window makeKeyAndVisible];
return YES;
}
第一个控制器初始化view:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"fristController";
label.font = [UIFont systemFontOfSize:17];
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
}
第二个控制器初始化view:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"secondController";
label.font = [UIFont systemFontOfSize:17];
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
}
主控制器逻辑实现
添加子控制器
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationItem.titleView = [self setupSegment];
self.fristVc = [[XCFristController alloc] init];
self.fristVc.view.frame = CGRectMake(0, navigationHeight, mainVcWidth, mainVcHeight - 64);
[self addChildViewController:_fristVc];
self.secondVc = [[XCSecondController alloc] init];
self.secondVc.view.frame = CGRectMake(0, navigationHeight, mainVcWidth, mainVcHeight - 64);
[self addChildViewController:_secondVc];
//设置默认控制器为fristVc
self.currentVC = self.fristVc;
[self.view addSubview:self.fristVc.view];
}
初始化UISegmentControl:
/**
* 初始化segmentControl
*/
- (UISegmentedControl *)setupSegment{
NSArray *items = @[@"1", @"2"];
UISegmentedControl *sgc = [[UISegmentedControl alloc] initWithItems:items];
//默认选中的位置
sgc.selectedSegmentIndex = 0;
//设置segment的文字
[sgc setTitle:@"oneView" forSegmentAtIndex:0];
[sgc setTitle:@"twoView" forSegmentAtIndex:1];
//监听点击
[sgc addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
return sgc;
}
监听segmentControl点击事件:
- (void)segmentChange:(UISegmentedControl *)sgc{
//NSLog(@"%ld", sgc.selectedSegmentIndex);
switch (sgc.selectedSegmentIndex) {
case 0:
[self replaceFromOldViewController:self.secondVc toNewViewController:self.fristVc];
break;
case 1:
[self replaceFromOldViewController:self.fristVc toNewViewController:self.secondVc];
break;
default:
break;
}
}
控制器切换
/**
* 实现控制器的切换
*
* @param oldVc 当前控制器
* @param newVc 要切换到的控制器
*/
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
/**
* transitionFromViewController:toViewController:duration:options:animations:completion:
* fromViewController 当前显示在父视图控制器中的子视图控制器
* toViewController 将要显示的姿势图控制器
* duration 动画时间(这个属性,old friend 了 O(∩_∩)O)
* options 动画效果(渐变,从下往上等等,具体查看API)UIViewAnimationOptionTransitionCrossDissolve
* animations 转换过程中得动画
* completion 转换完成
*/
[self addChildViewController:newVc];
[self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
if (finished) {
[newVc didMoveToParentViewController:self];
[oldVc willMoveToParentViewController:nil];
[oldVc removeFromParentViewController];
self.currentVC = newVc;
}else{
self.currentVC = oldVc;
}
}];
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现