• 利用SegmentedControl切换控制器的view


    #import "ViewController.h"

    #import "OneViewController.h"

    #import "TwoViewController.h"

     

    @interface ViewController ()

    @property (nonatomic, strong) UISegmentedControl *segmentedControl;

    @property (nonatomic, strong) UIViewController *currentViewController;

    @end

     @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

      self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"第一个视图",@"第二视图"]];

        self.segmentedControl.frame = CGRectMake(0, 0, 150, 30);

        self.navigationItem.titleView = self.segmentedControl;

        [self.segmentedControl addTarget:self action:@selector(segmentedControl:) forControlEvents:UIControlEventValueChanged];

        _segmentedControl.selectedSegmentIndex = 0;

        

        OneViewController *oneVC = [[OneViewController alloc] init];

        [self addChildViewController:oneVC];

        

        TwoViewController *twoVC = [[TwoViewController alloc] init];

        [self addChildViewController:twoVC];

        

        [self setSelectController:0];

    }

     

    - (void)segmentedControl:(UISegmentedControl*)scc{

        

        [self setSelectController:scc.selectedSegmentIndex];

    }

     

    - (void)setSelectController:(NSInteger)index{

        UIViewController *vc = [self.childViewControllers objectAtIndex:index];

        if (vc == self.currentViewController) {

            return;

        }

        [self.currentViewController.view removeFromSuperview];

        vc.view.frame = CGRectMake(0, 0, 320, 200);

        [self.view addSubview:vc.view];

         _currentViewController = vc;

    }

  • 相关阅读:
    CodeForces 1017B
    POJ 1323-Game Prediction(贪心)
    CodeForces 104B-Testing Pants for Sadness(思维题)
    CodeForces 1324D
    UVA 1152-4 Values whose Sum is 0(二分查找)
    POJ 1700-Crossing River(贪心)
    HDU 1789-Doing Homework again(并查集+贪心)
    SDUT 1298-活动选择(贪心)
    HDU 1272-小希的迷宫(带环并查集)
    CodeForces
  • 原文地址:https://www.cnblogs.com/qiumuan/p/5158225.html
Copyright © 2020-2023  润新知