使用容器控制器控制另外兩個控制器的view交換

來源:互聯網
上載者:User

標籤:des   style   blog   color   使用   os   

建三個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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.