菜鸟教程小白 发表于 2022-12-12 11:24:01

ios - UIAlertController -- 在此 block 中强烈捕获 'controller'


                                            <p><p><code>UIAlertController</code> 正在崩溃并显示以下错误消息:</p>

<blockquote>
<p>Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (&lt;UIAlertController: 0x7fb9107674d0&gt;)</p>
</blockquote>

<p>在 try catchtextFields objectAtIndex 时也会引发警告。 </p>

<p>有什么想法吗? </p>

<blockquote>
<p>Warning.. Capturing &#39;controller&#39; strongly in this block is likely to lead to a retail cycle. </p>
</blockquote>

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

<pre><code>-(void)viewWillAppear:(BOOL)animated
{
    // self.controller = ;

    UIAlertController* controller = ;
    [controller addTextFieldWithConfigurationHandler:^(UITextField * nametextField) {
      _nameTextField.text = .text;
    }];
    UIAlertAction *save = [UIAlertAction actionWithTitle:@&#34;Save Data&#34; style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
      ;
    }];
    UIAlertAction *cancel = ;
    ;
    ;
    ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>__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 = .text;

    // or

    weakTextField.text = .text;

}];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIAlertController -- 在此 block 中强烈捕获&#39;controller&#39;,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37347824/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37347824/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIAlertController -- 在此 block 中强烈捕获 &#39;controller&#39;