Let’s build a new oneNewViewController
, at the beginningViewController
Write the following code
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 50);
btn.backgroundColor = [UIColor brownColor];
[btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)jump{
NewViewController *newVC = [[NewViewController alloc]init];
[self presentViewController:newVC animated:YES completion:nil];
}
Then inNewViewController
Li:
-(void)dealloc{
NSLog(@"--------------dealloc");
}
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 300, 100, 50);
btn.backgroundColor = [UIColor brownColor];
[btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self log];
NSLog(@"--------------after");
});
}
-(void)log{
NSLog(@"--------------log");
}
-(void)jump{
NSLog(@"--------------dismiss");
[self dismissViewControllerAnimated:YES completion:nil];
}
After running, we jump into newvc and return. At this time:

The results show that when we disassiss, newvc has not been released,dealloc
Method indispatch_after
Delay method execution before leaving becausedispatch_after
Strong referenceSelf (i.e. newvc)
, and so on,self
To be released. Let’s godealloc
。
Next, we aredispatch_after
Inner handleFor self__ weak
Decorate, blockself
Change toweakself
, let’s follow the same operation process and see the results:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 300, 100, 50);
btn.backgroundColor = [UIColor brownColor];
[btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf log];
NSLog(@"--------------after");
});
}

When we use weak to modify self,dispatch_after
No strong referencesself
, so wedissmiss
When,dealloc
I’ll leave right away, and then 10 seconds later,dispatch_after
The execution function of will still execute. After is output, but log is not output because it is usedweakSelf
,dissmiss
After that, newvc has been released. At this time, the code[weakSelf log];
Equivalent to[nil log];
, so log will not be output.
Use attention
althoughdispatch_after
Call directly inself
It won’t cause circular references, but when wedispatch_after
When the delay time is too long, you need to consider whether to release the current object in time. If necessary, try to use itweakSelf
In this way, if you really need to use self to complete some operations and release them, you can write code as needed.