UISegmentedControl 分段器加载不同的viewcontroller
#import <UIKit/UIKit.h> @interface MJSegmentViewController : UIViewController
/**
* @brief 设置切换不同viewcontroller
*
* @param arrViewCtl 保存viewController对象
* @param arrTitle 分段器的标题
*/
- (void)loadSetViewController :(NSArray *)arrViewCtl andSegementTitle :(NSArray *)arrTitle; @end #import "MJSegmentViewController.h" @interface MJSegmentViewController () /**
* @brief 分段器选中加载对应的viewcontroller
*
*/
@property(nonatomic,assign) UIViewController *selectedViewController;
/**
* @brief 用来标识分段器选中的索引
*
*/
@property(nonatomic, assign) NSInteger selectedViewCtlWithIndex; /**
* @brief 声明UISegmentedControl 分段器
*/
@property(nonatomic,strong) UISegmentedControl *segmentControl;
@end @implementation MJSegmentViewController #pragma mark - lifeCircle
- (void)viewDidLoad
{
[super viewDidLoad]; [self createSegement]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]; } #pragma mark - private
/**
* @brief 创建分段器
*
*/
- (void)createSegement
{
if (!_segmentControl)
{
_segmentControl = [[UISegmentedControl alloc]init];
_segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
_segmentControl.layer.masksToBounds = YES ;
_segmentControl.layer.cornerRadius = 3.0; _segmentControl.tintColor= [UIColor colorWithRed:0.027
green:0.060
blue:0.099
alpha:0.900]; [_segmentControl setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.titleView = _segmentControl;
}
else
{
[_segmentControl removeAllSegments];
}
[_segmentControl addTarget:self
action:@selector(selectedSegmentClick:)
forControlEvents:UIControlEventValueChanged]; }
- (void)loadSetViewController:(NSArray *)arrViewCtl andSegementTitle:(NSArray *)arrTitle
{
if ([_segmentControl numberOfSegments] > )
{
return;
}
for (int i = ; i < [arrViewCtl count]; i++)
{
[self pushViewController:arrViewCtl[i] title:arrTitle[i]];
}
_segmentControl.frame = CGRectMake(, , , );
[_segmentControl setSelectedSegmentIndex:];
self.selectedViewCtlWithIndex = ; }
- (void)pushViewController:(UIViewController *)viewController title:(NSString *)title
{
[_segmentControl insertSegmentWithTitle:title atIndex:_segmentControl.numberOfSegments animated:NO];
[self addChildViewController:viewController];
[_segmentControl sizeToFit];
} - (void)setSelectedViewCtlWithIndex:(NSInteger)index
{
if (!_selectedViewController)
{
_selectedViewController = self.childViewControllers[index];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f)
{
CGFloat fTop = 20.0f;
if (self.navigationController && !self.navigationController.navigationBar.translucent)
{
fTop = self.navigationController.navigationBar.frame.size.height;
}
CGRect frame = self.view.frame;
[_selectedViewController view].frame = CGRectMake(frame.origin.x, frame.origin.y - fTop, frame.size.width, frame.size.height); }
else
{
[_selectedViewController view].frame = self.view.frame;
}
[self.view addSubview:[_selectedViewController view]];
[_selectedViewController didMoveToParentViewController:self];
}
else
{
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f) {
[self.childViewControllers[index] view].frame = self.view.frame;
}
[self transitionFromViewController:_selectedViewController toViewController:self.childViewControllers[index] duration:0.0f options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished)
{
_selectedViewController = self.childViewControllers[index];
_selectedViewCtlWithIndex = index;
}];
} } #pragma mark - action
- (void)selectedSegmentClick:(id)sender
{
self.selectedViewCtlWithIndex = _segmentControl.selectedSegmentIndex; } @end
#import <UIKit/UIKit.h>
#import "MJSegmentViewController.h"
@interface MJViewController : MJSegmentViewController @end
#import "MJViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface MJViewController () @end @implementation MJViewController - (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr = @[[self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"], [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]];
NSArray *title = @[@"one",@"two"];
[self loadSetViewController:arr andSegementTitle:title];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UISegmentedControl 分段器加载不同的viewcontroller的更多相关文章
- java类加载器加载文件
例子:采用配置文件加反射的方式创建ArrayList和HashSet的实例对象. //第一种方式:类加载器加载文件 InputStream ips = ReflectTest2.class.getCl ...
- JS模块加载器加载原理是怎么样的?
路人一: 原理一:id即路径 原则.通常我们的入口是这样的: require( [ 'a', 'b' ], callback ) .这里的 'a'.'b' 都是 ModuleId.通过 id 和路径的 ...
- 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行
1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
- 深入理解LINUX下动态库链接器/加载器ld-linux.so.2
[ld-linux-x86-64.so.2] 最近在Linux 环境下开发,搞了好几天 Compiler 和 linker,觉得有必要来写一篇关于Linux环境下 ld.so的文章了,google上搜 ...
- video.js分段自动加载视频【html5视频播放器】
突发奇想的需求,要在官网上放一个一个半小时的视频教程…… 然而,加载成了问题,页面是cshtml的.net混合网站,不知道哪儿的限制,导致视频加不出来. 没有办法,只能前端想办法了. 于是将视频切割成 ...
- Java类加载器加载类顺序
java ClassLoader的学习 java是一门解释执行的语言,由开发人员编写好的java源文件先编译成字节码文件.class形式,然后由java虚拟机(JVM)解释执 行,.class字节码文 ...
- 八爪鱼采集器︱加载更多、再显示20条图文教程(Xpatth、Ajax)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于代码布置采集器比较麻烦,又很早知道八爪鱼采 ...
- 使用类加载器加载配置文件/getClassLoader().getResourceAsStream()
加载配置文件的两种方式; 1. 2. 两者相比,第一种方式更为方便,因为文件目录发生改变就会找不到.
随机推荐
- 关于VSS配置遇到的问题及解决方法
今天安装网上的教程开始部署源代码管理器 相关工具 VSS安装包:http://url.cn/PolkN8 VSS汉化包:http://url.cn/PeHq1A 具体安装教程请参考:http://ww ...
- install python module
[install python module] 参考:http://docs.python.org/2.7/install/index.html
- B - Kefa and Company
B - Kefa and Company Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- TPARAMS和OLEVARIANT相互转换
所谓的“真3层”有时候是需要客户端上传数据集的TPARAMS到中间件的. 现在,高版本的DATASNAP的远程方法其实也是直接可以传输TPARAMS类型的变量,但是DELPHI7(七爷).六爷它们是不 ...
- 让MySQL支持中文
这两天在学习webpy,把webpy的一个blog例子扒下来学习一下,默认创建的table当存入中文的时候是乱码,研究了一下这个问题. 1,创建table的时候就使用utf8编码 举个例子: crea ...
- 删除qq历史签名
我们在设置新的个性签名的时候之前的签名会被记录,我们可以用手机qq删除这些历史签名,告别过去,做崭新的自己. 到需要删除的历史签名, 从右至左滑动屏幕
- to_number,Extract oracle的关键字
to_number(Extract(year from 字段名)) 简介:获取时间字段的年份后转换为数字
- POJ 3176 Cow Bowling (水题DP)
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
- 更有效率的使用Visual Studio(一)
很多比较通用的快捷键的默认设置其实是有一些缩写在里面的,这个估计也是MS帮助我们记忆.比如说注释代码的快捷键是Ctrl + E + C,我们如果知道它是 Ctrl + Edit + Comment C ...
- WinForm控件使用文章收藏整理完成
对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...