【问题标题】:UIAlertController -- Capturing 'controller' strongly in this blockUIAlertController -- 在此块中强烈捕获“控制器”
【发布时间】:2016-05-20 13:21:21
【问题描述】:

UIAlertController 正在崩溃并显示此错误消息:

不允许在解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为 ()

在尝试捕获 textFields objectAtIndex 时也会引发警告。

有什么想法吗?

警告.. 在此区块中强烈捕获“控制器”可能会导致零售周期。

我还尝试创建一个 @property (weak) 引用,警告消失了,但应用程序仍然因此崩溃:

-(void)viewWillAppear:(BOOL)animated
{
    // self.controller = [UIAlertController alloc];

    UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"Add Alergy To List" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [controller addTextFieldWithConfigurationHandler:^(UITextField * nametextField) {
        _nameTextField.text = [controller.textFields objectAtIndex:0].text;
    }];
    UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save Data" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [self save:nil];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel Action" style:(UIAlertActionStyleCancel) handler:nil];
    [controller addAction:save];
    [controller addAction:cancel];
    [self presentViewController:controller animated:YES completion:nil];
}

【问题讨论】:

  • 在您看来,_nameTextField.text = [controller.textFields objectAtIndex:0].text; 的那一行实际上是什么?

标签: ios objective-c uialertcontroller


【解决方案1】:
__weak __typeof(UIAlertController) weakController = controller;
__weak __typeof(UITextField) weakTextField = _nameTextField;

[controller addTextFieldWithConfigurationHandler:^(UITextField *nametextField){

    // nameTextField is the textField being returned in the block
    // if you want to use _nameTextField you need to make that weak too

    nameTextField.text = [weakController.textFields objectAtIndex:0].text;

    // or

    weakTextField.text = [weakController.textFields objectAtIndex:0].text;

}];

【讨论】:

    【解决方案2】:

    添加
    __weak __typeof(UIAlertController) weakController = controller;
    在你的addTextFieldWithConfigurationhandler:之前。

    那么你应该替换

    _nameTextField.text = [controller.textFields objectAtIndex:0].text;
    

    nameTextField.text = [weakController.textFields objectAtIndex:0].text;
    

    没有_。
    _ 用于属性的内部访问。在这里您必须更新参数中给出的字段

    事实是你在一个块中,如果你想从控制器字段中获取文本,你必须对它进行弱引用。弱控制器将阻止增加保留计数。

    【讨论】:

    • 嗨,伙计们,非常感谢您的帮助,所有错误都消失了,但应用程序并没有崩溃!困惑为什么 nameTextField 一直显示为 null。
    【解决方案3】:

    您正在尝试在分配视图控制器之前加载警报控制器此警告是因为您将 UIAlertController 直接添加到 viewWillAppear

    在视图控制器的视图即将添加到视图层次结构之前调用此方法

    1. 只需花一些时间让视图添加到视图层次结构中。使用 dispatch_after 或其他一些延迟功能来做到这一点。
    2. 由于您在块内获取文本字段输入,因此您不能使用 strong 声明,因此您必须先声明 __weak typeof(UIAlertController) *weakcontroller = controller;进入区块。查看苹果开发者网站here了解更多。

    整体修改代码如下:

    -(void)viewWillAppear:(BOOL)animated{
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           UIAlertController* controller = [UIAlertController alertControllerWithTitle:@"Add Alergy To List" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
           __weak typeof(UIAlertController) *weakcontroller = controller;
           [controller addTextFieldWithConfigurationHandler:^(UITextField * nametextField) {
            nametextField.text = [weakcontroller.textFields objectAtIndex:0].text;
           }];
           UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save Data" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [self save:nil];
            }];
           UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel Action" style:(UIAlertActionStyleCancel) handler:nil];
           [controller addAction:save];
           [controller addAction:cancel];
           [self presentViewController:controller animated:YES completion:nil];
       });
    }
    

    它可以正常工作。

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2011-12-30
      • 2011-11-04
      • 1970-01-01
      相关资源
      最近更新 更多