コンテナコントローラを使用して他の2つのコントローラのview交換を制御
12167 ワード
3つのUIDIewControllerのサブコントローラを構築し、そのうちの1つはルートコントローラであり、他の2つのコントローラのビューは切り替え対象とする.
AppDelegateのコード
ルートビューコントローラRootView Controlのコード
BlueViewControlのコード
RedViewController
AppDelegateのコード
//AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@end
//AppDelegate.m
ルートビューコントローラRootView Controlのコード
//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
BlueViewControlのコード
//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