• UIViewController添加子控制器(addChildViewController)


    //

    //  TaskHallViewController.m

    //  yybjproject

    //

    //  Created by bingjun on 15/10/27.

    //  Copyright © 2015 bingjun. All rights reserved.

    //

     

    #import "TaskHomeHallViewController.h"

    #import "UINavigationBar+Awesome.h"

    #import "HomeViewController.h"

    #import "moreSensationViewController.h"

     

    #import "SVSegmentedControl.h"

    #import "TaskListViewController.h"

    #import "MyTaskViewController.h"

    #import "TaskHallViewController.h"

     

    @interface TaskHomeHallViewController ()

    {

        MyTaskViewController *_myTaskVc;///< 红人任务控制器

        

        moreSensationViewController *_moreSensationVc;///< 红人榜单控制器

        TaskHallViewController *_shopTrolleyVc;///< 清单界面

        

        NSInteger _segmentSelectedIndex;///< 当前segmentindex

        

        UIButton *leftBtn;///< 城市的名字

    }

    @end

     

    @implementation TaskHomeHallViewController

     

    #pragma mark - tileView的一些设置

    - (void)segmentedControlChangedValue:(SVSegmentedControl*)segmentedControl {

        

    //    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

        segmentedControl.userInteractionEnabled = NO;

        if (_segmentSelectedIndex == segmentedControl.selectedSegmentIndex) {

    //        [[UIApplication sharedApplication] endIgnoringInteractionEvents];

            segmentedControl.userInteractionEnabled = YES;

            return;

        }

        UIViewController *beforeVc;

        switch (_segmentSelectedIndex) {

            case 0:

                beforeVc = _myTaskVc;

                break;

            case 1:

                beforeVc = _shopTrolleyVc;

                break;

            case 2:

                beforeVc = _moreSensationVc;

                break;

            default:

                break;

        }

        //启动后先添加first,大儿子

        switch (segmentedControl.selectedSegmentIndex) {

            case 0:

            {

                _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

                //调用addChildViewController之前会自动调用secondwillMoveToParentViewController

                [self addChildViewController:_myTaskVc];

                

                [_myTaskVc.view setFrame:_myTaskVc.view.frame];

                

                [self transitionFromViewController:beforeVc toViewController:_myTaskVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                    //删除之前first之前手动添加的willMoveToParentViewController

                    [beforeVc willMoveToParentViewController:nil];

                    //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                    [beforeVc removeFromParentViewController];

                    //addChildViewController之后手动添加的didMoveToParentViewController

                    [_myTaskVc didMoveToParentViewController:self];

    //                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                    segmentedControl.userInteractionEnabled = YES;

                }];

                break;

            }

            case 1:

            {

                _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

                [self addChildViewController:_shopTrolleyVc];

                

                [_shopTrolleyVc.view setFrame:_myTaskVc.view.frame];

                [self transitionFromViewController:beforeVc toViewController:_shopTrolleyVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                    //addChildViewController之后手动添加的didMoveToParentViewController

                    //删除之前first之前手动添加的willMoveToParentViewController

                    [beforeVc willMoveToParentViewController:nil];

                    //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                    [beforeVc removeFromParentViewController];

                    [_shopTrolleyVc didMoveToParentViewController:self];

    //                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                    segmentedControl.userInteractionEnabled = YES;

                }];

                break;

            }

            case 2:

            {

                _segmentSelectedIndex = segmentedControl.selectedSegmentIndex;

                [self addChildViewController:_moreSensationVc];

                [_moreSensationVc.view setFrame:_myTaskVc.view.frame];

                [self transitionFromViewController:beforeVc toViewController:_moreSensationVc duration:1.0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished){

                    //删除之前first之前手动添加的willMoveToParentViewController

                    [beforeVc willMoveToParentViewController:nil];

                    //调用removeFromParentViewController之后会自动调用firstdidMoveToParentViewController

                    [beforeVc removeFromParentViewController];

                    //addChildViewController之后手动添加的didMoveToParentViewController

                    [_moreSensationVc didMoveToParentViewController:self];

    //                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                    segmentedControl.userInteractionEnabled = YES;

                }];

                break;

            }

            default:

                break;

        }

        

    }

     

    //设置titleVIew

    - (void)initTitleViewOfMyTask

    {

        NSArray *array;

        if (kDeviceWidth > 320) {

            array = [NSArray arrayWithObjects:@"  ", @"  ", @"  ", nil];

        }else {

            array = [NSArray arrayWithObjects:@"任务", @"红人", @"榜单", nil];

        }

        

        SVSegmentedControl *yellowRC = [[SVSegmentedControl alloc] initWithSectionTitles:array];

        [yellowRC addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];

        //    yellowRC.crossFadeLabelsOnDrag = YES;

        yellowRC.animateToInitialSelection = YES;

        yellowRC.font = [UIFont systemFontOfSize:14];

        yellowRC.textColor = [UIColor whiteColor];//没有被选中的字体颜色

        //    yellowRC.titleEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 14);

        yellowRC.height = 30;

        yellowRC.backgroundTintColor = YSRedProjectColor;//没有被选中的背景颜色

        //  yellowRC.textShadowColor = [UIColor whiteColor];

        [yellowRC setSelectedSegmentIndex:0 animated:NO];

        yellowRC.thumb.tintColor = [UIColor whiteColor];//选中的背景颜色

        yellowRC.thumb.textColor = YSRedProjectColor;//选中的title字体颜色

        yellowRC.thumb.textShadowColor = [UIColor whiteColor];

        //    yellowRC.thumb.textShadowOffset = CGSizeMake(0, 1);

        yellowRC.thumb.gradientIntensity = 0.05;

        yellowRC.center = CGPointMake(180, 100);

        

        self.navigationItem.titleView = yellowRC;

        _segmentSelectedIndex = 0;

    }

     

    //添加右上角按钮

    - (void)addLeftBarButtonItemAction

    {

        CGFloat kLefyViewWidth;

        CGFloat kImageViewWidth;

        CGFloat kTitleViewWidth;

        CGFloat kTitleFontSize;

        if (kDeviceWidth > 320) {

            kLefyViewWidth = 75.0f;

            kImageViewWidth = 14.0f;

            kTitleViewWidth = 60.0f;

            kTitleFontSize = 13.0f;

        }else {

            kLefyViewWidth = 60.0f;

            kImageViewWidth = 10.0f;

            kTitleViewWidth = 50.0f;

            kTitleFontSize = 10.0f;

        }

        

        UIView *rightItemView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kLefyViewWidth, 34.0f)];

        rightItemView.backgroundColor = [UIColor clearColor];

        

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, (34 - kImageViewWidth) / 2.0f, kImageViewWidth, kImageViewWidth)];

        imageView.image = [UIImage imageNamed:@"positionIcon.png"];

        imageView.contentMode = UIViewContentModeScaleAspectFit;

        [rightItemView addSubview:imageView];

        

        leftBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

        

        User *user = [[User instance] getUserInformation];

        if (user.userCity && ![user.userCity isKindOfClass:[NSNull class]]) {

            [leftBtn setTitle:user.userCity forState:UIControlStateNormal];

        }else {

            [leftBtn setTitle:@"暂无" forState:UIControlStateNormal];

        }

        leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

        leftBtn.frame=CGRectMake(kImageViewWidth, 0.0f, kTitleViewWidth, 34.0f);

        [leftBtn.titleLabel setFont:[UIFont systemFontOfSize:kTitleFontSize]];

        [rightItemView addSubview:leftBtn];

        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:rightItemView];

        self.navigationItem.leftBarButtonItem = leftItem;

    }

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

        

        [self addLeftBarButtonItemAction];

        

        //设置导航栏颜色

        self.navigationController.navigationBar.translucent = YES;

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

            self.edgesForExtendedLayout = UIRectEdgeNone;

        }

        [self.navigationController.navigationBar lt_setBackgroundColor:YSRedProjectColor];

        

        HomeViewController *home = [HomeViewController singleton];

        //    HomeViewController *home = self.navigationController.tabBarController;

        [home showTabBar:YES];

        

        //设置titleView

        [self initTitleViewOfMyTask];

        

        //生俩儿子

        _myTaskVc = [[MyTaskViewController alloc] initWithNibName:@"MyTaskViewController" bundle:nil];

        _moreSensationVc = [[moreSensationViewController alloc] initWithNibName:nil bundle:nil];

        _shopTrolleyVc = [[TaskHallViewController alloc] initWithNibName:@"TaskHallViewController" bundle:nil];

        _shopTrolleyVc.isNeedShowSearchFlag = YES;

        

        //启动后先添加first,大儿子

        [self addChildViewController:_myTaskVc];

        [_myTaskVc.view setFrame:self.view.frame];

        [self.view addSubview:_myTaskVc.view];

        [_myTaskVc didMoveToParentViewController:self];

        

        [[NSNotificationCenter defaultCenter]addObserver:self

                                                selector:@selector(update_CityName:)

                                                    name:Update_CityName object:nil];

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

     

    - (void)viewWillAppear:(BOOL)animated

    {

        [super viewWillAppear:animated];

        

        HomeViewController *home = [HomeViewController singleton];

        [home showTabBar:NO];

    }

     

    - (void)viewWillDisappear:(BOOL)animated

    {

        [super viewWillDisappear:animated];

        

        HomeViewController *home = [HomeViewController singleton];

        [home hideTabBar:NO];

    }

     

    - (void)update_CityName:(NSNotification *)notification

    {

         NSString *cityName = notification.object;

        [leftBtn setTitle:cityName forState:UIControlStateNormal];

    }

    @end

  • 相关阅读:
    root登录出现“sorry, that didn't work please try again”
    【自适应辛普森】积分计算
    【CF1553F】Pairwise Modulo
    调和级数的复杂度
    CF 1600-2000 的思维题
    中超热身赛(2021湘潭全国邀请赛-重现)补题
    牛客2021年度训练联盟热身训练赛第一场(讲题)
    新知识-Queue_循环队列
    新知识-valueOf(Leetcode 1556_千位分隔符)
    新知识-位运算(Leetcode 217_存在重复元素)
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/4929314.html
Copyright © 2020-2023  润新知