如何在iOS中的UIAlertView中添加3个UITextField?

时间:2016-01-13 14:22:29

标签: ios objective-c uitextfield uialertview

我想在UIAlertView中添加3个TextFields,例如oldpassword,Newpassword,confirmmpassword就像这样。 这是我试过的代码

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

它只显示两个文本字段。

3 个答案:

答案 0 :(得分:4)

使用UIAlertController。

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

答案 1 :(得分:0)

swift 2.0:

1)制作公共变量

var alertController: UIAlertController?

2)设置警报控制器

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)取TextField值

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)
}

注意:

答案 2 :(得分:0)

这是正确的方法:

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

现在添加文本字段

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
    }

添加第一个动作

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 ?? "")
    })

添加取消操作

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

向警报控制器添加操作

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

现在存在alertController

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

将此代码放入您的函数中,使用您的参数进行修改,然后在任何需要的地方调用它...