daria-kopaliani / DAAlertController

DAAlertController is pretty much a UIAlertController for both iOS 8 and 7.
MIT License
50 stars 8 forks source link

Question: passing result of textField.text into DAAlertAction #5

Open Mamonaku opened 9 years ago

Mamonaku commented 9 years ago

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 commented 9 years ago

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 ?

yao23 commented 9 years ago

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/

damaex commented 9 years ago

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;
                                 }];
BootMaker commented 8 years ago

@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.