码迷,mamicode.com
首页 > 其他好文 > 详细

UISegmentedControl 分段器加载不同的viewcontroller

时间:2015-04-19 19:28:17      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

#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:@"2"] 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] > 0)
    {
        return;
    }
    for (int i = 0; i < [arrViewCtl count]; i++)
    {
        [self pushViewController:arrViewCtl[i] title:arrTitle[i]];
    }
    _segmentControl.frame = CGRectMake(0, 0, 200, 25);
    [_segmentControl setSelectedSegmentIndex:0];
    self.selectedViewCtlWithIndex = 0;



}
- (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

标签:

原文地址:http://www.cnblogs.com/thbbsky/p/4439574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!