最近项目中需要用到输入关键字的功能,于是想到了在弹框中输入的方式,节省屏幕空间,效果更好一点.
首先,项目赶得急,于是百度相关的三方库,可是效果都不是太理想,刚好看到有一篇文章介绍到UIAlertController中有textFields属性,可以添加多个输入框,于是果断自己写了.
参考了一篇文章(http://www.bubuko.com/infodetail-1172959.html),写起来还是很简单的,效果也不错,
代码如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入关键词" preferredStyle:UIAlertControllerStyleAlert];
//增加确定按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField * firstKeywordTF = [[UITextField alloc]init];
UITextField * secondKeywordTF = [[UITextField alloc]init];
UITextField * ThirdKeywordTF = [[UITextField alloc]init];
NSArray * textFieldArr = @[firstKeywordTF,secondKeywordTF,ThirdKeywordTF];
textFieldArr = alertController.textFields;
UITextField * tf1 = alertController.textFields[0];
UITextField * tf2 = alertController.textFields[1];
UITextField * tf3 = alertController.textFields[2];
NSLog(@" %@,%@ ,%@",tf1.text,tf2.text,tf3);
_keyWordsL.text = [NSString stringWithFormat:@"%@ %@ %@",tf1.text,tf2.text,tf3.text];
}]];
//增加取消按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
//定义第一个输入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第一个关键词";
}];
//定义第二个输入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第二个关键词";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第三个关键词";
}];
[self presentViewController:alertController animated:true completion:nil];