iOS 导航条上添加UISegmentedControl,点击分类按钮,显示不同的ViewController

https://blog.csdn.net/ycf03211230/article/details/54095838


#import "NextViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"

#define COLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]

@interface NextViewController (){
    NSMutableDictionary *listDic;
}

@property(nonatomic, strong) OneViewController *oneVC;
@property(nonatomic, strong) TwoViewController *twoVC;
@property(nonatomic, strong) ThreeViewController *threeVC;

@property (nonatomic ,strong) UIViewController *currentVC;

@end

@implementation NextViewController

-(OneViewController *)oneVC{
    if (_oneVC == nil) {
        _oneVC = [[OneViewController alloc] init];
    }
    return _oneVC;
}


-(TwoViewController *)twoVC{
    if (_twoVC == nil) {
        _twoVC = [[TwoViewController alloc] init];
    }
    return _twoVC;
}


-(ThreeViewController *)threeVC{
    if (_threeVC == nil) {
        _threeVC = [[ThreeViewController alloc] init];
    }
    return _threeVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    listDic = [NSMutableDictionary dictionary];

    NSArray * _titles = @[@"申请", @"待办",@"测试"];
    UISegmentedControl * _segmentedControl = [[UISegmentedControl alloc] initWithItems:_titles];
    _segmentedControl.selectedSegmentIndex = 0;
    _segmentedControl.tintColor = COLOR(183, 222, 232, 1.0);

    //修改字体的默认颜色与选中颜色
    //选择后的字体颜色(在NSDictionary中 可以添加背景颜色和字体的背景颜色)
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],
                         NSForegroundColorAttributeName,
                         [UIFont systemFontOfSize:12],
                         NSFontAttributeName,nil];

    [ _segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];

    //默认字体颜色
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:COLOR(183, 222, 232, 1.0),
                          NSForegroundColorAttributeName,
                          [UIFont systemFontOfSize:12],
                          NSFontAttributeName,nil];

    [ _segmentedControl setTitleTextAttributes:dic1 forState:UIControlStateNormal];

    [_segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];

    _segmentedControl.frame = CGRectMake(0.0, 0.0, 200.0, 29.0);
    self.navigationItem.titleView = _segmentedControl;

    //将默认的页面添加进去
    [self addChildViewController:self.oneVC];

    [self.view addSubview:self.oneVC.view];
    //设置当前显示的VC是哪个
    self.currentVC = _oneVC;


}

//按钮点击事件
-(void)segmentValueChanged:(UISegmentedControl *)seg{
    NSLog(@"seg.tag-->%ld",seg.selectedSegmentIndex);
    switch (seg.selectedSegmentIndex) {
        case 0:
            [self replaceController:self.currentVC newController:self.oneVC];
            break;
        case 1:
            [self replaceController:self.currentVC newController:self.twoVC];
            break;
        case 2:
            [self replaceController:self.currentVC newController:self.threeVC];
            break;

        default:
            break;
    }

    //NSArray *sub = self.childViewControllers;
    //NSLog(@"sub-->%@",sub);
}


//  切换各个标签内容
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    /**
     *            着重介绍一下它
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController      当前显示在父视图控制器中的子视图控制器
     *  toViewController        将要显示的姿势图控制器
     *  duration                动画时间
     *  options                 动画效果
     *  animations              转换过程中得动画
     *  completion              转换完成
     */
    if ([newController isEqual:self.currentVC]) {
        return;
    }

    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {

        if (finished) {

            [self.view addSubview:newController.view];
            self.currentVC = newController;

        }else{

            self.currentVC = oldController;

        }
    }];
}

 

转载于:https://my.oschina.net/huqiji/blog/3009121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值