• 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:@"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

     

  • 相关阅读:
    如下架构的调整就是一种“移动算力”的情形
    越是松散和动态的,越要进行适当和合理的控制,不然会造成很多麻烦;(权衡之道-利弊(相依相存)分析)
    缓存属于哪类的解决方案
    对需求的理解深度影响你的设计水平:
    日志埋点不可少的点
    资源的使用:能节省的地方一定要节省,达不到节省条件的不能节省,该怎么用就怎么用(不要因为存储影响了计算,存储容量不是技术问题)?
    设置多少线程合适?并不是依据你cpu的核数而定的?
    网络服务超时分析:
    思考:网络性能优化:网络 -- cpu -- 线程数 -- 单个任务耗时 --- qps --- 并发
    python selenium 处理时间日期控件
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4439574.html
Copyright © 2020-2023  润新知