presentViewController后,在dismiss动画执行完前vc就被释放,会导致vc的view不被释放。

dimiss动画结束前,如果vc就被释放了,
会导致vc的view不被释放。

#import "ViewController.h"

#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];
    
    {
        UIButton *btnStep1 = [UIButton new];
        btnStep1.frame = CGRectMake(10, 160, 300, 30);
        [self.view addSubview:btnStep1];
        btnStep1.backgroundColor = [UIColor redColor];
        [btnStep1 addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];
        [btnStep1 setTitle:@"first step .present " forState:UIControlStateNormal];
    }
    
    {
        UIButton *btnStep2 = [UIButton new];
        btnStep2.frame = CGRectMake(10, 260, 300, 30);
        [self.view addSubview:btnStep2];
        btnStep2.backgroundColor = [UIColor redColor];
        [btnStep2 addTarget:self action:@selector(letVcDealloc) forControlEvents:UIControlEventTouchUpInside];
        [btnStep2 setTitle:@"second step. vc be dealloced" forState:UIControlStateNormal];
    }
}

-(void)letVcDealloc{
    /*
     vc释放了,dealloc会被调用。
     但是vc的view没有释放,用Debug View Hierarchy中可以看到,view还在。
     怀疑是因为present的动画强引用了view。
    */
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        
        //会导致vc被释放
        ViewController *vc = [ViewController new];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.window setRootViewController:vc];
    }
    
    //这样不会有问题。
    /*
    [self dismissViewControllerAnimated:YES completion:^{
        ViewController *vc = [ViewController new];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.window setRootViewController:vc];
    }];
     */
}

-(void)presentAction{
    ViewController *vc = [ViewController new];
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)dealloc{
    NSLog(@"dealloc");
}

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值