DAAlertController icon indicating copy to clipboard operation
DAAlertController copied to clipboard

Question: passing result of textField.text into DAAlertAction

Open Mamonaku opened this issue 9 years ago • 4 comments

Hi Daria,

congrats on this very useful component ! I'd like to have one of the textField.text

send back to

    DAAlertAction *signUpAction = [DAAlertAction actionWithTitle:@"Ok" style:DAAlertActionStyleDefault handlerWithResponse:^(NSDictionary *response) {

        DLog(@"response %@",response);

    }];

where response is a dictionary with @{@"1":@"value of textField number 1"} etc....

In your code DAAlertController.m there is

            if (action.handler) {
                action.handler();
            }

to which I can add

            if (action.handlerWithResponse) {
            UITextField* tf = <get TextField number one>
                action.handlerWithResponse(@{@"1":tf.text});
            }

My question to you is: Where do I get to find the textField attributes in DAAlertController.m ?

thank you !

Mamonaku avatar May 25 '15 12:05 Mamonaku

mmm ... not sure this is going to fly... this is the action configuration, is there like a call back function that is always called by DAActionController, prior to firing the selected action ?

Mamonaku avatar May 25 '15 13:05 Mamonaku

if you want 2 text boxes, you can use login style, but set second item secureTextEntry to NO, refer to http://forums.macrumors.com/threads/uialertviews-with-textfields.1355954/

yao23 avatar Jun 06 '15 22:06 yao23

I handled it like this:

__block UITextField* nameHandler;

DAAlertAction *cancelAction = [DAAlertAction actionWithTitle:@"Cancel" style:DAAlertActionStyleDestructive handler:nil];
DAAlertAction *okAction = [DAAlertAction actionWithTitle:@"OK" style:DAAlertActionStyleDefault handler:^{
    // work with it
    NSString * username = nameHandler.text;
}];

[DAAlertController showAlertViewInViewController:self
                                       withTitle:@"Title"
                                         message:@"Message"
                                         actions:@[okAction, cancelAction]
                              numberOfTextFields:1
                  textFieldsConfigurationHandler:^(NSArray *textFields) {
                      nameHandler = [textFields firstObject]; // set the pointer
                  }
                                 validationBlock:^BOOL(NSArray *textFields) {
                                     UITextField *nickNameTextField = [textFields firstObject];
                                     return nickNameTextField.text.length >= 3;
                                 }];

damaex avatar Jul 21 '15 12:07 damaex

@damaex 's solution is a correct one with the usage of __block as we'd like to use a variable outside the scope of the block. I suppose it's a weakness of this polished code so I advice to Daria to include a more elegant version for this evident problem.

BootMaker avatar Oct 31 '15 06:10 BootMaker