多控制器

 如果发现控制器的View还在,但是view上面的数据不显示,极大可能是因为:控制器为局部变量,提前销毁了,需要创建强引用

#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];
    
//    HMTestViewController *test = [[HMTestViewController alloc] init];
//    test.view.frame = CGRectMake(100, 100, 200, 300);
//    test.view.backgroundColor = [UIColor redColor];
//    [self.view addSubview:test.view];
//    self.test = test;
    
    // 如果发现:控制器的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 {

    // 强引用, remove view不会销毁
    [self.two.view removeFromSuperview];
    [self.one.view removeFromSuperview];
    [self.view addSubview:self.three.view];
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值