先上圖挺物,實(shí)現(xiàn)效果如下,用的系統(tǒng)最原始提示輸入框
輸入框.jpeg
代碼非常簡(jiǎn)單锚国,就不多說(shuō)了托享,直接把代碼貼出來(lái)
let alertCon = UIAlertController.init(title:"輸入標(biāo)簽", message:"對(duì)這個(gè)地方的描述", preferredStyle:.alert)
let confirmAction = UIAlertAction.init(title:"確定", style:UIAlertActionStyle.default, handler: { (a) in
let textField = alertCon.textFields?.first()
// 點(diǎn)擊確定后頁(yè)面對(duì)于輸入框內(nèi)容的處理邏輯
...
})
alertCon.addAction(confirmAction)
alertCon.addAction(UIAlertAction.init(title:"取消", style:.cancel, handler:nil))
alertCon.addTextField(configurationHandler: { (textField) in
textField.placeholder = "隨便說(shuō)點(diǎn)兒什么吧..."
})
self.present(alertCon, animated:true, completion:nil)
如果你想有兩個(gè)甚至多個(gè)輸入框的話可以繼續(xù)加入
let alertCon = UIAlertController.init(title:"輸入標(biāo)簽", message:"對(duì)這個(gè)地方的描述", preferredStyle:.alert)
let confirmAction = UIAlertAction.init(title:"確定", style:UIAlertActionStyle.default, handler: { (a) in
let textField0 = alertCon.textFields?.first()
// 點(diǎn)擊確定后對(duì)第一個(gè)輸入框中文字的處理
let textField1 = alertCon.textFields![1]
// 點(diǎn)擊確定后對(duì)第二個(gè)輸入框中文字的處理
print(textField1.text)
let textField2 = alertCon.textFields![2]
// 點(diǎn)擊確定后對(duì)第三個(gè)輸入框中文字的處理
print(textField2.text)
})
alertCon.addAction(confirmAction)
alertCon.addAction(UIAlertAction.init(title:"取消", style:.cancel, handler:nil))
// 對(duì)于第一個(gè)textfield進(jìn)行配置
alertCon.addTextField(configurationHandler: { (textField) in
textField.placeholder = "隨便說(shuō)點(diǎn)兒什么吧..."
// 可以設(shè)置代理,控制輸入的字?jǐn)?shù)以及鍵盤類型等等
})
// 對(duì)于第二個(gè)textfield進(jìn)行配置
alertCon.addTextField(configurationHandler: { (textfield) in
textfield.placeholder = "不說(shuō)"
textfield.isSecureTextEntry = true
textfield.borderStyle = .line
})
// 對(duì)于第三個(gè)textfield進(jìn)行配置
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:@"請(qǐng)輸入個(gè)人信息" preferredStyle:UIAlertControllerStyleAlert];
//增加確定按鈕;
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//獲取第1個(gè)輸入框顶瞒;
UITextField *userNameTextField = alertController.textFields.firstObject;
//獲取第2個(gè)輸入框夸政;
UITextField *passwordTextField = alertController.textFields.lastObject;
NSLog(@"用戶名 = %@,密碼 = %@",userNameTextField.text,passwordTextField.text);
}]];
//增加取消按鈕榴徐;
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
//定義第一個(gè)輸入框守问;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請(qǐng)輸入用戶名";
}];
//定義第二個(gè)輸入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請(qǐng)輸入密碼";
}];
[self presentViewController:alertController animated:true completion:nil];