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. 两者相比,第一种方式更为方便,因为文件目录发生改变就会找不到.
随机推荐
- [转]比较 Rational Unified Process (RUP) 和 Microsoft Solutions Framework (MSF)
文档选项 将此页作为电子邮件发送 级别: 初级 Sandra Sergi Santos, 软件工程专家, IBM 2007 年 6 月 15 日 本文来自于 Rational Edge:Micro ...
- HDU 5164Matching on Array(AC自动机)
这是BC上的一道题,当时比赛没有做,回头看看题解,说是AC自动机,想着没有写过AC自动机,于是便试着抄抄白书的模板,硬是搞了我数个小时2000ms时限1800过了= = ! 这里就直接贴上BC的结题报 ...
- .Net项目版本号的生成
给.Net项目编译的程序集加入版本号的方式有许多种,包括: 1. 默认的方式,在每个项目的AssemblyInfo.cs文件中指定版本号: // Version information for an ...
- libevent中定时器的使用方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h ...
- Could not load file or assembly 'MagickNet.dll'
1 确定项目中bin目录下存在该DLL文件 2 安装 VC++发布组件_缩略图用_x86(1).exe
- jedis提供的功能
Sorting(排序) Connection handling(连接池) Commands operating on any kind of values Commands operating on ...
- [转]C语言文件输入/输出ACM改进版(freopen函数)
C语言文件输入/输出ACM改进版(freopen函数) 2009年5月27日 10:379,457 浏览数发表评论阅读评论 文章作者:姜南(Slyar) 文章来源:Slyar Home (www. ...
- VS 2010下单元测试
1.创建单元测试项目 2.创建完成后,新建项目会自动添加“Microsoft.VisualStudio.QualityTools.UnitTestFramework”的引用,该引用用于单元 ...
- INNO&&DELPHI
好久不弄delphi了,再次见到inno,居然不知所措~ 让人亲切的 var,又见到了.而如今,到处可见. 整目录复制 Flags: igNoreversion recursesubdirs crea ...
- ActionBar ShareActionProvider
添加share按钮 添加share按钮的主要步骤: 1. 在ActionBar中添加share按钮 2. 从item中获取ShareActionProvider ShareActionProvider ...