iOS-分段控件控制页面切换
来源:互联网 发布:淘宝售前和售后的区别 编辑:程序博客网 时间:2024/05/18 01:00
原理:
把分段控件定义在导航栏标题视图上,在通过分段控件的响应事件的选中索引加载各个控制器视图在self.view上
注意:要在AppDelegate中定义包含导航栏的入口,让ViewController继承导航栏控制器,分段控件不能作为导航栏的标题视图的
实现代码:
新建FirstViewController和SecondViewController两个控制器
AppDelegate.m文件的方法中加入口@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //创建窗口 _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; //背景颜色 _window.backgroundColor = [UIColor whiteColor]; //可视化 [_window makeKeyAndVisible]; ViewController *VC = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC]; _window.rootViewController = nav; return YES;}
#import "ViewController.h"#import "FirstViewController.h"#import "SecondViewController.h"@interface ViewController (){ FirstViewController *firstVC; SecondViewController *secondVC;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor lightGrayColor]; UISegmentedControl *segmt = [[UISegmentedControl alloc] initWithItems:@[@"首页",@"第一个",@"第二个"]]; segmt.frame = CGRectMake(0, 0, 200, 50); segmt.tintColor = [UIColor blueColor]; segmt.selectedSegmentIndex = 0; [segmt addTarget:self action:@selector(segmtAction:) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView = segmt; firstVC = [[FirstViewController alloc] init]; secondVC = [[SecondViewController alloc] init]; firstVC.view.backgroundColor = [UIColor redColor]; secondVC.view.backgroundColor = [UIColor greenColor];}- (void) segmtAction:(UISegmentedControl *)segmt { int index = (int)segmt.selectedSegmentIndex; switch (index) { case 0: [firstVC.view removeFromSuperview]; [secondVC.view removeFromSuperview]; break; case 1: [self.view addSubview:firstVC.view]; [secondVC.view removeFromSuperview]; break; case 2: [self.view addSubview:secondVC.view]; [firstVC.view removeFromSuperview]; break; default: break; }}@end
运行效果:
0 0
- iOS-分段控件控制页面切换
- iOS 页面切换控制
- react-native试玩(16)-iOS分段控制控件
- iOS--分段控件UISegmentedControl
- IOS-添加分段控件SegmentControl
- 仿IOS分段控件SegmentConrtol
- IOS-添加分段控件SegmentControl
- IOS开发:使用animateWithDuration简单地控制页面切换效果
- iOS页面之间切换
- IOS页面切换
- UISegmentedControl 分段控件-IOS开发
- UISegmentedControl 分段控件-IOS开发
- UISegmentedControl 分段控件-IOS开发
- ios之分段控件和开关
- UISegmentedControl 分段控件-iOS开发
- UISegmentedControl 分段控件-IOS开发
- iOS———-分段控件(UISegmentedControl)
- UISegmentedControl 分段控件-IOS开发
- 提高数据库处理查询速度
- Android常用的工具方法(2)
- 将十进制转换为任意进制的数
- CSS块级元素和行内元素
- Spring源码分析【8】-MyBatis注解方法不能重载
- iOS-分段控件控制页面切换
- System.out.println() 和 log4j 的 Logger 循环输出100万次。
- Excel Sheet Column Number
- PHP开发面试题目总结
- tornado 异步非阻塞 实验
- 金蝶打印时选择使用套打秒退
- linux菜鸟学习----MySQL导入.sql文件及常用命令
- DIV+CSS右列宽度自适应布局的不同实现方法
- 关于Keil编译后Code RO RW ZI !