dickfala dickfala - 7 months ago 27
iOS Question

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

How can I pass the textview value to the alertController textFields block.

I have the textview component call "myTextView"

@property (weak, nonatomic) IBOutlet UITextView *myTextView


I set the alertcontroller below:

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];


But I set the myTextView.text in the myAlertController block,

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


}];

It will show

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


How can I get the textview value in the alert controller block for textfield use?

thank you very much.

Answer Source

You will want to create a weak reference to self before accessing myTextView inside your block:

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

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

}];
...