• 使用容器控制器控制另外两个控制器的view交换


    建三个UIViewController 的子控制器,其中一个为根控制器,另外两个控制器的视图作为切换对象

    AppDelegate中代码

    //AppDelegate.h中代码
    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (retain, nonatomic) UIWindow *window;
    
    @end
    //AppDelegate.m中代码

    根视图控制器RootViewController中代码

    //RootViewController.h中代码
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    //RootViewController.m中代码
    #import "RootViewController.h"
    #import "BlueViewController.h"
    #import "RedViewController.h"
    @interface RootViewController ()
    {
        BlueViewController *_blueview;
        RedViewController *_redview;
    }
    @end
    
    @implementation RootViewController
    -(void)dealloc
    {
        [_blueview release];
        [_redview release];
        [super dealloc];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    - (void)change:(UIButton *)button
    {
        if (_blueview.view.superview !=nil) {
            [_blueview.view removeFromSuperview];
            [self.view addSubview:_redview.view];
        }else{
            [_redview.view removeFromSuperview];
            [self.view addSubview:_blueview.view];
        }
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //蓝色视图控制器的创建
        _blueview = [[BlueViewController alloc] init];
        _blueview.view.frame = CGRectMake(0, 0, 320, 440);
    
        //添加到根控制器
        [self.view addSubview:_blueview.view];
        
        //红色视图控制器创建
        _redview = [[RedViewController alloc] init];
        _redview.view.frame = CGRectMake(0, 0, 320, 440);
    //添加到根控制器
        [self.view addSubview:_redview.view];
        
        //在根控制器上创建带触发交换事件的按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(100, 440, 120, 40);
        [button setTitle:@"交换" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor blackColor];
        [button addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        // 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中代码

    //BlueViewController.h中代码
    #import <UIKit/UIKit.h>
    
    @interface BlueViewController : UIViewController
    
    @end
    //BlueViewController.m中代码
    #import "BlueViewController.h"
    
    @interface BlueViewController ()
    
    @end
    
    @implementation BlueViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (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

    RedViewController

    //RedViewController.h中代码
    #import <UIKit/UIKit.h>
    
    @interface RedViewController : UIViewController
    
    @end
    //RedViewController.m中代码
    #import "RedViewController.h"
    
    @interface RedViewController ()
    
    @end
    
    @implementation RedViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (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
  • 相关阅读:
    【测试】模拟一个全表扫描的sql,对其进行优化走索引,并且将执行计划稳定到baseLine。
    【练习】手工生成awr报告
    【测试】数据文件的删除恢复
    【练习】行迁移和行链接
    织梦栏目列表页第一个文章与其他文章不同样式
    织梦联动筛选【单选版】-支持手机站使用
    织梦联动枚举字段无二级时去掉多余下拉
    织梦联动类型地区联动三级修复以及省份-市级-地区分开+高亮
    织梦联动枚举字段添加一级分类如果超过132个自动变成二级修复教程
    织梦后台自定义表单联动地区显示为数字的真正解决方法
  • 原文地址:https://www.cnblogs.com/limicheng/p/3840451.html
Copyright © 2020-2023  润新知