UI3_视图切换

//
//  ViewController.m
//  UI3_视图切换
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    FourthViewController *fouthVC = [[FourthViewController alloc] init];
    
    [self addChildViewController:secondVC];
    [self addChildViewController:thirdVC];
    [self addChildViewController:fouthVC];
    
    NSLog(@"count = %li", self.childViewControllers.count);
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(20, 100, self.view.frame.size.width-40, 50);
    btn1.backgroundColor = [UIColor whiteColor];
    [btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn1.tag = 100;
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(20, 200, self.view.frame.size.width-40, 50);
    btn2.backgroundColor = [UIColor whiteColor];
    [btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 101;
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn3.frame = CGRectMake(20, 300, self.view.frame.size.width-40, 50);
    btn3.backgroundColor = [UIColor whiteColor];
    [btn3 setTitle:@"按钮3" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn3.tag = 102;
    [self.view addSubview:btn3];
}


- (void)btnClicked:(UIButton *)btn
{
    //self.childViewControllers
    if (btn.tag==100) {
        [self.view addSubview:((UIViewController *)[self.childViewControllers objectAtIndex:0]).view];
    }
    else if(btn.tag ==101)
    {
        [self.view addSubview:((UIViewController *)[self.childViewControllers objectAtIndex:1]).view];
    }
    else if(btn.tag ==102)
    {
        [self.view addSubview:((UIViewController *)[self.childViewControllers objectAtIndex:2]).view];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//
//  FourthViewController.m
//  UI3_视图切换
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 100, self.view.frame.size.width-40, 50);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClicked
{
    if (self.view.superview) {
        //移除子视图
        [self.view removeFromSuperview];
    }
}
- (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.
}
*/

@end

 

//
//  ThirdViewController.m
//  UI3_视图切换
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 100, self.view.frame.size.width-40, 50);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClicked
{
    if (self.view.superview) {
        //移除子视图
        [self.view removeFromSuperview];
    }
}

- (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.
}
*/

@end

 

//
//  SecondViewController.m
//  UI3_视图切换
//
//  Created by zhangxueming on 15/7/3.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blueColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 100, self.view.frame.size.width-40, 50);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

- (void)btnClicked
{
    if (self.view.superview) {
        //移除子视图
        [self.view removeFromSuperview];
    }
}

- (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.
}
*/

@end

 

posted @ 2015-07-11 14:33  sirzhang  阅读(271)  评论(0编辑  收藏  举报
AmazingCounters.com