zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    ASP.NET Forms身份认证
    .net中调用存储过程的示例 带有输出参数
    jquery ajax .net使用
    存储过程实现批量删除
    相关分页Select语句(扩展)
    css中对于文字溢出时的控制
    精打细算过日子 车辆养用如何能更省钱
    2013年最新交通法规全文 具体扣分细节全面解读
    wxpython学习笔记(一)
    wxpython学习笔记(一)
  • 原文地址:https://www.cnblogs.com/0515offer/p/4638609.html
Copyright © 2011-2022 走看看