when viewController is an container

本文探讨了iOS应用开发中视图控制器的正确管理方式,包括如何避免内存泄漏,特别是涉及父子视图控制器间的交互与生命周期管理。通过具体示例说明了不同场景下正确的视图控制器管理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

起因:  

    

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)buttonAction:(UIButton *)button
{
   //添加界面
    NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];
//    [self addChildViewController:secondViewController];
    [self.view addSubview:secondViewController.view];
//    [secondViewController didMoveToParentViewController:self];
    
}

在firstviewcontroller上添加secondViewController

    [secondviewcontroller.view  removeFromSuperview];的时候,程序奔溃

原因分析:

    当添加secondviewController.view的时候,

     系统增加啦viewcontroll的属性view的引用计数,减少啦secondviewcontroller的引用计数,此时:

    NDTSecondViewController delloc

     viewcontrolller 已经释放,

    当secondviewcontroller.view  removeFromSuperview];的时候,系统减少view的引用计数,同时减少viewcontroller的引用计数,崩溃;


解决办法:

     把viewcontroller当做容器的时候

    [self addChildViewController:secondViewController];

    [self.view addSubview:secondViewController.view];

    [secondViewController didMoveToParentViewController:self];

     先把secondviewcontroller添加到容器中。。。。

   

     当secondviewcontroller移除的时候


//删除界面

    [self.viewremoveFromSuperview];

    [selfremoveFromParentViewController];

此时:secondviewController会自动释放

系统会自动调用:  [secondViewController didMoveToParentViewController:nil];

无需手动调用,详情见官方文档


当使用下面这种方式的时候,    secondViewController dismiss的时候,secondViewController会自动释放;

 [selfpresentViewController:secondViewControlleranimated:YEScompletion:^{

        NSLog(@"complete");

    }];

[selfdismissViewControllerAnimated:YEScompletion:^{}];


nav  push ,pop同上

添加:

当一个全屏的viewcontroller A想要添加一个size 大小为300 300 的viewController的时候 ,用上述方法也是可以的,而且不影响viewController的生命周期

代码如下:

 NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    secondViewController.view.frame =  CGRectMake(0, 0, 500, 500);
//    secondViewController.view.frame = CGRectMake(0, 0, 500, 500);

    [self addChildViewController:secondViewController];
    [self.view addSubview:secondViewController.view];
    [nav didMoveToParentViewController:self];



三、当childviewcontroller.view添加在containercontroller.view的子视图上的时候,会不会有啥内存或其他方面的问题呢?

验证:

    

//添加界面

    NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];

    

    secondViewController.view.frame = self.smallView.bounds;

    [self addChildViewController:secondViewController];

    [self.smallView addSubview:secondViewController.view];

    [secondViewController didMoveToParentViewController:self];

答案:不影响生命周期,内存规则同上述一致;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值