#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的更多相关文章

  1. java类加载器加载文件

    例子:采用配置文件加反射的方式创建ArrayList和HashSet的实例对象. //第一种方式:类加载器加载文件 InputStream ips = ReflectTest2.class.getCl ...

  2. JS模块加载器加载原理是怎么样的?

    路人一: 原理一:id即路径 原则.通常我们的入口是这样的: require( [ 'a', 'b' ], callback ) .这里的 'a'.'b' 都是 ModuleId.通过 id 和路径的 ...

  3. 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行

    1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...

  4. JVM自定义类加载器加载指定classPath下的所有class及jar

    一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...

  5. 深入理解LINUX下动态库链接器/加载器ld-linux.so.2

    [ld-linux-x86-64.so.2] 最近在Linux 环境下开发,搞了好几天 Compiler 和 linker,觉得有必要来写一篇关于Linux环境下 ld.so的文章了,google上搜 ...

  6. video.js分段自动加载视频【html5视频播放器】

    突发奇想的需求,要在官网上放一个一个半小时的视频教程…… 然而,加载成了问题,页面是cshtml的.net混合网站,不知道哪儿的限制,导致视频加不出来. 没有办法,只能前端想办法了. 于是将视频切割成 ...

  7. Java类加载器加载类顺序

    java ClassLoader的学习 java是一门解释执行的语言,由开发人员编写好的java源文件先编译成字节码文件.class形式,然后由java虚拟机(JVM)解释执 行,.class字节码文 ...

  8. 八爪鱼采集器︱加载更多、再显示20条图文教程(Xpatth、Ajax)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于代码布置采集器比较麻烦,又很早知道八爪鱼采 ...

  9. 使用类加载器加载配置文件/getClassLoader().getResourceAsStream()

    加载配置文件的两种方式; 1. 2. 两者相比,第一种方式更为方便,因为文件目录发生改变就会找不到.

随机推荐

  1. SpringBoard

    [SpringBoard] Springboard, or Home Screen, is the standard application that manages the iOS home scr ...

  2. tomcat的 JNDI 配置

    tomcat的conf/server.xml 配置 尽量用简单版 <Context path="/cas" docBase="D:\YC\zqV7\cas\WebR ...

  3. JSF 2 button and commandButton example

    In JSF 2.0, both <h:button /> and <h:commandButton /> tags are used to render HTML input ...

  4. jfinal的ajax例子

    @(编程) 简介 JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful. 在拥有Java语言所有 ...

  5. 乱侃c++

    就在刚才我感觉c++真的好复杂,函数重载,多态,虚函数,虚函数表,模版,继承等一大坨东西好恶心,c++既然完全支持C语言,当然是把它的优缺点统统接下了,C语言中指针本身并不太难,是C语言的精华,当年刚 ...

  6. hdoj 5288 OO’s Sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 //*************头文件区************* #include<ios ...

  7. 关于C#中文本模板(.tt)的简单应用

    这两天做项目突遇 .tt文件,之前没有接触过,so查询学习做笔记,帮助记忆和后来者. 在项目添加中点击选择文本模板 下面贴出代码,做了简单的注释 <#@ template debug=" ...

  8. SiteMesh学习入门

    http://www.java3z.com/cwbwebhome/article/article2/2962.html?id=1668 demo下载 简介:     sitemesh应用Decorat ...

  9. Java *字格

    class XingDemo { public static void main(String[] args) { int j = 0; int i = 0; int k = 0; for(i = 0 ...

  10. hdu 5268 ZYB loves Score 水题

    ZYB loves Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...