UIday04_zy:容器视图控制器练习


AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController * rootVC = [[RootViewController alloc]init];
    self.window.rootViewController = rootVC;
    
    return YES;
}

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@property(nonatomic,strong)RootView * rv;
@property(nonatomic,strong)RedViewController * redVC;
@property(nonatomic,strong)BlueViewController * blueVC;

@end

@implementation RootViewController

-(void)loadView{
    self.rv = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建视图容器
    self.redVC = [[RedViewController alloc]init];
    self.blueVC = [[BlueViewController alloc]init];
    
    //自定义视图
    UIView * contentView = [[UIView alloc]init];
    contentView.frame = CGRectMake(0, CGRectGetMaxX(self.view.frame)-60, CGRectGetWidth(self.view.frame), 60);
    contentView.backgroundColor = [UIColor grayColor];
    contentView.tag = 101;
    [self.view addSubview:contentView];
    
    //用自定义视图装redButton
    UIButton * redButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [redButton setTitle:@"红色" forState:UIControlStateNormal];
    [redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    redButton.backgroundColor = [UIColor grayColor];
    redButton.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame)/2.0, 60);
    [redButton addTarget:self action:@selector(redButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:redButton];
    
    //用自定义视图装blueButton
    UIButton * blueButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [blueButton setTitle:@"蓝色" forState:UIControlStateNormal];
    [blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    blueButton.backgroundColor = [UIColor whiteColor];
    blueButton.frame = CGRectMake(CGRectGetMaxX(redButton.frame), CGRectGetMinY(redButton.frame), CGRectGetWidth(redButton.frame), CGRectGetHeight(redButton.frame));
    [blueButton addTarget:self action:@selector(blueButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:blueButton];
    
    
    //容器视图器
    //确定关系
    [self addChildViewController:self.redVC];
    [self addChildViewController:self.blueVC];
    
    //显示视图
    [self.view addSubview:self.redVC.view];
    
    [self.view bringSubviewToFront:contentView];
    
}

//点击事件方法
//点击button切换视图
-(void)redButtonAction:(UIButton *)sender{
    NSLog(@"红");
    [self transitionFromViewController:self.blueVC toViewController:self.redVC duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        [self.view bringSubviewToFront:[self.view viewWithTag:101]];
    } completion:^(BOOL finished) {
        
    }];
}

-(void)blueButtonAction:(UIButton *)sender{
    NSLog(@"蓝");
    [self transitionFromViewController:self.redVC toViewController:self.blueVC duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
        [self.view bringSubviewToFront:[self.view viewWithTag:101]];
    } completion:^(BOOL finished) {
        
    }];
}


- (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

RootView.m

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}

@end

RedViewController.m

#import "RedViewController.h"

@interface RedViewController ()

@end

@implementation RedViewController

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

- (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

BlueViewController.m

#import "BlueViewController.h"

@interface BlueViewController ()

@end

@implementation BlueViewController

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

- (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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值