【发布时间】:2017-11-02 00:55:16
【问题描述】:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self doSomething];
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
我了解周期。 self 保留UIAlertController,UIAlertController 保留UIAlertAction,UIAlertAction 保留self
我的意思是,这个类在内部不能设计为在运行 UIAlertAction 之一后释放所有内容吗?
-
为了澄清,我知道可以通过使用对 self 的弱引用来避免这个问题。
我要问的是,为什么 UIAlertController 在用户选择了一个动作后不直接清除所有动作(以及它们的处理程序块)。这将打破循环并避免我们需要做的整个弱者之舞。
这样的……
@implementation UIAlertController
...
// An action button was pressed
- (void)actionSelectedIndex:(NSInteger)index
{
UIAlertAction *action = self.actions[index];
action.handler(action); // Run the action handler block
self.actions = nil; // Release all the actions
}
【问题讨论】:
标签: ios memory-management objective-c-blocks uialertcontroller retain-cycle