先上圖,實現(xiàn)效果如下,用的系統(tǒng)最原始提示輸入框
輸入框.jpeg
代碼非常簡單,就不多說了,直接把代碼貼出來
let alertCon = UIAlertController.init(title:"輸入標簽", message:"對這個地方的描述", preferredStyle:.alert)
let confirmAction = UIAlertAction.init(title:"確定", style:UIAlertActionStyle.default, handler: { (a) in
let textField = alertCon.textFields?.first()
// 點擊確定后頁面對于輸入框內容的處理邏輯
...
})
alertCon.addAction(confirmAction)
alertCon.addAction(UIAlertAction.init(title:"取消", style:.cancel, handler:nil))
alertCon.addTextField(configurationHandler: { (textField) in
textField.placeholder = "隨便說點兒什么吧..."
})
self.present(alertCon, animated:true, completion:nil)
如果你想有兩個甚至多個輸入框的話可以繼續(xù)加入
let alertCon = UIAlertController.init(title:"輸入標簽", message:"對這個地方的描述", preferredStyle:.alert)
let confirmAction = UIAlertAction.init(title:"確定", style:UIAlertActionStyle.default, handler: { (a) in
let textField0 = alertCon.textFields?.first()
// 點擊確定后對第一個輸入框中文字的處理
let textField1 = alertCon.textFields![1]
// 點擊確定后對第二個輸入框中文字的處理
print(textField1.text)
let textField2 = alertCon.textFields![2]
// 點擊確定后對第三個輸入框中文字的處理
print(textField2.text)
})
alertCon.addAction(confirmAction)
alertCon.addAction(UIAlertAction.init(title:"取消", style:.cancel, handler:nil))
// 對于第一個textfield進行配置
alertCon.addTextField(configurationHandler: { (textField) in
textField.placeholder = "隨便說點兒什么吧..."
// 可以設置代理,控制輸入的字數(shù)以及鍵盤類型等等
})
// 對于第二個textfield進行配置
alertCon.addTextField(configurationHandler: { (textfield) in
textfield.placeholder = "不說"
textfield.isSecureTextEntry = true
textfield.borderStyle = .line
})
// 對于第三個textfield進行配置
alertCon.addTextField(configurationHandler: { (textfield) in
textfield.placeholder = "哈哈哈"
textfield.isSecureTextEntry = true
textfield.borderStyle = .bezel
})
self.present(alertCon, animated:true, completion:nil)
效果如下圖
WechatIMG74.jpeg
oc 版本
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"請輸入個人信息" preferredStyle:UIAlertControllerStyleAlert];
//增加確定按鈕;
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//獲取第1個輸入框;
UITextField *userNameTextField = alertController.textFields.firstObject;
//獲取第2個輸入框;
UITextField *passwordTextField = alertController.textFields.lastObject;
NSLog(@"用戶名 = %@,密碼 = %@",userNameTextField.text,passwordTextField.text);
}]];
//增加取消按鈕;
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
//定義第一個輸入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入用戶名";
}];
//定義第二個輸入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入密碼";
}];
[self presentViewController:alertController animated:true completion:nil];