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