0

I want to add 3 TextFields in UIAlertView like oldpassword, Newpassword, confirmpassword like this. Here is my code i tried

-(IBAction)changePswd:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Change Password" message:@"Enter Your New Password" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"submit", nil];
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alertView textFieldAtIndex:0]setSecureTextEntry:YES];
[alertView textFieldAtIndex:0].keyboardType = UIKeyboardTypeDefault;
[alertView textFieldAtIndex:0].returnKeyType = UIReturnKeyDone;
[[alertView textFieldAtIndex:0]setPlaceholder:@"Enter Your Old Password"];
[[alertView textFieldAtIndex:1]setPlaceholder:@"Enter password"];
[[alertView textFieldAtIndex:2]setPlaceholder:@"Re-Enter Password"];
[alertView show];
}

it shows only two textfields.

Victor
  • 3,669
  • 3
  • 37
  • 42
User558
  • 1,165
  • 1
  • 13
  • 37
  • What os are you targeting? You should look at UIAlertControlle: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/ – Peter Hornsby Jan 13 '16 at 14:45
  • Possible duplicate of [How to add multiple UITextFields to a UIAlertView in iOS 7?](http://stackoverflow.com/questions/19090547/how-to-add-multiple-uitextfields-to-a-uialertview-in-ios-7) – Avijit Nagare Jan 13 '16 at 14:53
  • You should make a custom alert to add textfields, just make a custom UIView class and make it look like what you want. Then just show it in your class instead of calling UIAlertView. – MSU_Bulldog Jan 13 '16 at 15:32
  • thank for your reply's:) – User558 Jan 14 '16 at 05:51

3 Answers3

4

Use UIAlertController for this.

UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:title
                                      message:message
                               preferredStyle:UIAlertControllerStyleAlert];

__block typeof(self) weakSelf = self;

//old password textfield
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.tag = 1001;
     textField.delegate = weakSelf;
     textField.placeholder = @"Old Password";
     textField.secureTextEntry = YES;
     [textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:)
         forControlEvents:UIControlEventEditingChanged];
 }];

//new password textfield
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.tag = 1002;
     textField.delegate = weakSelf;
     textField.placeholder = @"New Password";
     textField.secureTextEntry = YES;

     }];

//confirm password textfield
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
 {
     textField.tag = 1003;
     textField.delegate = weakSelf;
     textField.placeholder = @"Confirm Password";
     textField.secureTextEntry = YES;
     [textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:)
         forControlEvents:UIControlEventEditingChanged];
 }];

[self presentViewController:alertController animated:YES completion:nil];

#pragma mark - UITextField Delegate Methods
- (void)alertTextFieldDidChange:(UITextField *)sender
{
   alertController = (UIAlertController *)self.presentedViewController;
   UITextField *firstTextField = alertController.textFields[0];
}
rushisangani
  • 3,195
  • 2
  • 14
  • 18
0

swift 2.0 :

1) Making a public variable

var alertController: UIAlertController?

2) Setting up Alert Controller

alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .Alert)

    //old password textfield
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
        textField.placeholder = "Password"
        textField.secureTextEntry = true
        textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
    })
    //new password textfield
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in
        textField.tag = 1002
        textField.placeholder = "New Password"
        textField.secureTextEntry = true
        textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
    })
    //confirm password textfield
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in
        textField.tag = 1003
        textField.placeholder = "Confirm Password"
        textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
    })
    alertController!.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
        //Do after submit is clicked
    }))
alertController!.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
    self.alertController?.dismissViewControllerAnimated(true, completion: nil)
}))
    self.presentViewController(alertController!, animated: true, completion: nil)

3) Taking TextField values

func alertTextFieldDidChange(sender: UITextField) {
    alertController = self.presentedViewController as? UIAlertController
    let firstTextField: UITextField = (alertController?.textFields![0])!
    let secondTextField: UITextField = (alertController?.textFields![1])!
    let thirdTextField: UITextField = (alertController?.textFields![2])!

    print(firstTextField.text)
    print(secondTextField.text)
    print(thirdTextField.text)
}

Note:

Alvin George
  • 14,148
  • 92
  • 64
0

this is the right way:

let title = "Your Title"
let message = "Your message"
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

now add textfields

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Your placeholder..."
        textField.tintColor = .yourColor
        textField.secureTextEntry = true
    }
    alertController.addTextField { (textField2 : UITextField!) -> Void in
        textField2.placeholder = "Your placeholder2..."
        textField2.tintColor = . yourColor
        textField2.secureTextEntry = true
    }

add first action

let firstAction = UIAlertAction(title: "Save Text", style: .default, handler: { alert -> Void in
        guard let textField = alertController.textFields else { return }
        let firstTextField = textField[0] as UITextField
        let secondTextField = textField[1] as UITextField
        //insert your code here
        print(secondTextField.text ?? "") 
        print(firstTextField.text ?? "")
    })

add Cancel action

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancel) in
        print("Action cancelled")
    }

add actions to alert controller

alertController.addAction(firstAction)
alertController.addAction(cancelAction)

now present alertController

self.present(alertController, animated: true, completion: nil)

put this code in your function, modify with your parameters and call it wherever you want...

Fabio
  • 5,432
  • 4
  • 22
  • 24