![](/img/trans.png)
[英]How to add multiple UITextFields to a UIAlertView in iOS 7?
[英]How to add 3 UITextFields into UIAlertView in iOS?
我想在UIAlertView
添加3個TextField,例如oldpassword,Newpassword,confirmpassword。 這是我嘗試過的代碼
-(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];
}
它僅顯示兩個文本字段。
為此使用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];
}
迅捷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)
}
注意:
這是正確的方法:
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)
將此代碼放入您的函數中,使用您的參數進行修改,然后在任何需要的地方調用它...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.