【发布时间】:2017-10-03 13:46:25
【问题描述】:
我有一个简单的方法来显示带有文本字段的 AlertView。仪器显示内存泄漏。请解释一下。
- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:@"key"];
if (value == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
alertView.tag = 101;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtGroup = [alertView textFieldAtIndex:0];
[txtGroup becomeFirstResponder];
[alertView show];
alertView = nil;
}
}
【问题讨论】:
-
你为什么要让 alertView = nil ?展示后?
-
我在某处读到,释放一个需要在使用后设置为 nil 的对象。
-
是的。同意,但是当您显示警报视图时,意味着它仍然在层次结构中,并且您将立即使其为零。在 ARC 中,它会处理它。想做的话,用完后再做
-
如何从层次结构中删除它并释放(dealloc)它?什么时候?
标签: ios memory-leaks automatic-ref-counting uialertview instruments