我正在尝试做一些实验。
- (IBAction)btn1Action:(id)sender {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"popvc2id" sender:self];
});
NSLog(@"TAP");
}
当点击按钮时,将花费1秒执行segue,而再次轻击此按钮将触发两次segue,因此将创建两个
ViewController
实例。在工具中,我可以看到两个实例,但其中一个是
leaked VC object
。现在我想做的是
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
VC2 *vc2 = [segue destinationViewController];
[ary addObject:vc2];
if(ary.count > 1) {
VC2 *vc = (VC2*)ary[1];
vc = nil;
[ary removeObjectAtIndex:1];
}
[ary removeAllObjects];
NSLog(@"-> %@", vc2);
}
保留
VC objects
的记录并尝试销毁第二个obj,这样就可以防止内存泄漏。但是它不起作用,我该如何解决?
最佳答案
如果您想取消以前的请求。我的建议是使用NSObject CancelPreviousRequest
方法
实施方法:
- (IBAction)btn1Action:(id)sender {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayedAction) object:nil];
[self performSelector:@selector(delayedAction) withObject:nil afterDelay:1];
}
-(void)delayedAction{
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"popvc2id" sender:self];
});
}
关于ios - 试图防止内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40237345/