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

上一篇:Android studio配置git


下一篇:移动端网站开发前端学习总结