// HMViewController.m
// 控制器的view的显示
//
// Created by apple on 14-10-10.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HMViewController.h"
#import "HMOneViewController.h"
#import "HMTwoViewController.h"
#import "HMThreeViewController.h"
@interface HMViewController ()
- (IBAction)vc1;
- (IBAction)vc2;
- (IBAction)vc3;
@property (nonatomic, strong) HMTestViewController *test;
@property (nonatomic, strong) HMOneViewController *one;
@property (nonatomic, strong) HMTwoViewController *two;
@property (nonatomic, strong) HMThreeViewController *three;
@end
@implementation HMViewController
- (HMOneViewController *)one
{
if (!_one) {
self.one = [[HMOneViewController alloc] init];
self.one.view.frame = CGRectMake(10, 70, 300, 300);
}
return _one;
}
- (HMTwoViewController *)two
{
if (!_two) {
self.two = [[HMTwoViewController alloc] init];
self.two.view.frame = CGRectMake(10, 70, 300, 300);
}
return _two;
}
- (HMThreeViewController *)three
{
if (!_three) {
self.three = [[HMThreeViewController alloc] init];
self.three.view.frame = CGRectMake(10, 70, 300, 300);
}
return _three;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 如果发现:控制器的view还在,但是view上面的数据不显示,极大可能是因为:控制器被提前销毁了
// 1.一个控制器的view是可以随意调整尺寸和位置的
// 2.一个控制器的view是可以随意添加到其他view中
// 3.如果将一个控制器的view,添加到其他view中显示,那么要想办法保证控制器不被销毁
// 4.原则:只要view在,view所在的控制器必须得在,这样才能保证view内部的数据和业务逻辑正常
}
- (IBAction)vc1 {
[self.two.view removeFromSuperview];
[self.three.view removeFromSuperview];
[self.view addSubview:self.one.view];
}
- (IBAction)vc2 {
[self.one.view removeFromSuperview];
[self.three.view removeFromSuperview];
[self.view addSubview:self.two.view];
}
- (IBAction)vc3 {
[self.two.view removeFromSuperview];
[self.one.view removeFromSuperview];
[self.view addSubview:self.three.view];
}
@end
// 控制器的view的显示
//
// Created by apple on 14-10-10.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "HMViewController.h"
#import "HMOneViewController.h"
#import "HMTwoViewController.h"
#import "HMThreeViewController.h"
@interface HMViewController ()
- (IBAction)vc1;
- (IBAction)vc2;
- (IBAction)vc3;
@property (nonatomic, strong) HMTestViewController *test;
@property (nonatomic, strong) HMOneViewController *one;
@property (nonatomic, strong) HMTwoViewController *two;
@property (nonatomic, strong) HMThreeViewController *three;
@end
@implementation HMViewController
- (HMOneViewController *)one
{
if (!_one) {
self.one = [[HMOneViewController alloc] init];
self.one.view.frame = CGRectMake(10, 70, 300, 300);
}
return _one;
}
- (HMTwoViewController *)two
{
if (!_two) {
self.two = [[HMTwoViewController alloc] init];
self.two.view.frame = CGRectMake(10, 70, 300, 300);
}
return _two;
}
- (HMThreeViewController *)three
{
if (!_three) {
self.three = [[HMThreeViewController alloc] init];
self.three.view.frame = CGRectMake(10, 70, 300, 300);
}
return _three;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 如果发现:控制器的view还在,但是view上面的数据不显示,极大可能是因为:控制器被提前销毁了
// 1.一个控制器的view是可以随意调整尺寸和位置的
// 2.一个控制器的view是可以随意添加到其他view中
// 3.如果将一个控制器的view,添加到其他view中显示,那么要想办法保证控制器不被销毁
// 4.原则:只要view在,view所在的控制器必须得在,这样才能保证view内部的数据和业务逻辑正常
}
- (IBAction)vc1 {
[self.two.view removeFromSuperview];
[self.three.view removeFromSuperview];
[self.view addSubview:self.one.view];
}
- (IBAction)vc2 {
[self.one.view removeFromSuperview];
[self.three.view removeFromSuperview];
[self.view addSubview:self.two.view];
}
- (IBAction)vc3 {
[self.two.view removeFromSuperview];
[self.one.view removeFromSuperview];
[self.view addSubview:self.three.view];
}
@end