iOS 将 textview 值设为 alertController.textFields。在警报控制器块中

iOS take textview value to alertController.textFields. in alert controller block

如何将 textview 值传递给 alertController textFields 块?

我有textview组件调用"myTextView"

@property (weak, nonatomic) IBOutlet UITextView *myTextView

我在下面设置了警报控制器:

myAlertController = [UIAlertController alertControllerWithTitle:@"title" message:@"pass the value in alertcontroller" preferredStyle: UIAlertControllerStyleAlert];
myAlertControllerOKAction = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault
                                                    handler:nil];
[myAlertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.text = [self.myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}];
[myAlertController addAction: myAlertControllerOKAction];

但是我在 myAlertController 块中设置了 myTextView.text,

textField.text = [self.myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

}];

会显示

Capturing 'self' strongly in this block is likely to lead to a retain cycle

如何在警报控制器块中获取文本视图值以供文本字段使用?

像这样在块外创建一个 "self" 的弱变量:

Swift 3:

weak var w_self = self

Objective-C:

__weak MyObject *weakSelf = self;

然后在块内使用这个而不是 "self"。

在您的块内访问 myTextView 之前,您需要创建对 self 的弱引用:

...
__weak __typeof(self) weakSelf = self;
[myAlertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.keyboardType = UIKeyboardTypeDefault;
    textField.text = [weakSelf.myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

}];
...

在块中使用弱self

__weak typeof(self) weakSelf = self;

[myAlertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.text = [weakSelf.myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}];

如果在区块中使用强self,可能会导致self无法释放

Always pass weak reference of self into block in ARC?

就像其他人指出的那样,在 Swift 或 ObjC 中,您可以创建一个对象的 weak 引用以避免闭包内的强保留循环。

ObjC:

__weak typeof(self) weakSelf = self;
[myAlertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.text = [weakSelf.myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}];

Swift weak:

myAlertController.addTextField(configurationHandler: { [weak self] (textField : UITextField!) -> Void in
    // Notice the guard to make sure myTextView is unwrapped
    guard let weakTextView = weak.myTextView,
        let weakText = weakTextView.text,
        weakText.characters.count > 0 else {
        return
    }
    textField.keyboardType .default
    textField.text = weakText.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
})

Swift unowned:

myAlertController.addTextField(configurationHandler: { [unowned self] (textField : UITextField!) -> Void in
    // Notice the use of unowned, unlike weak, unowned does not have to be unwrapped to avoid nil

    textField.keyboardType .default
    textField.text = unowned.weakTextView.text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
})

在带有 unowned 的示例中,注意到我没有像处理弱引用那样解开 unowned 引用。这是因为 unowned 不是可选的,我们不会 运行 使用 unowned 时 运行 变成 nil 的风险。我个人更喜欢使用 unowned