#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. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  2. Swift-CALayer十则示例

    作者:Scott Gardner   译者:TurtleFromMars原文:CALayer in iOS with Swift: 10 Examples 如你所知,我们在iOS应用中看到的都是视图( ...

  3. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  4. StackOverflowError 和 OutOfMemoryError

    package cn.zno.outofmomery; import java.util.ArrayList; import java.util.List; public class Test { v ...

  5. java(2014版)连接数据库的工具类

    package util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...

  6. Server-U与IIS端口占用问题解决

    在新版的ftp软件Server-U(11.X)中,由于其默认设置中监听了80端口,经常会导致IIS不能服务不能正常启用,下面记录如何修改server-u的80端口.     打开Server-U软件, ...

  7. .net 下的MVCPager

    http://www.cnblogs.com/jiagoushi/archive/2012/12/16/2820835.html http://blog.itpub.net/9914816/views ...

  8. Flex编程注意之直接获取某个组件的对象(this[]用法)通过id获取控件

    有这样一个需求:假如你new了一百次Button,同时这些button的id分别赋值如btn1.id = "button1"; btn2.id = "button2&qu ...

  9. C#操作注册表全攻略

    相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了.这东西对Windows系统来说可是比较重要的,也是病 毒常常会光顾的地方,比如病毒和恶意软件常常会在注册表的启动项 ...

  10. c语言write与python的struct模块交互

    以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...