记录今天遇到的一个小需求,要在弹窗上面增加一个输入框,有两种实现方法。
第一种:创建一个UIView在view上添加UITextfield和按钮
第二种:利用系统的UIAlertController增加UITextfield。
这里简单介绍一下第二种方法,具体代码如下:
@property (nonatomic,strong) UITextField *TextField;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加选项" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//增加取消按钮
UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//增加确定按钮
UIAlertAction *alertB = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//获取第1个输入框;
self.TextField = alertController.textFields.firstObject;
//限制输入框字数
[self.TextField addTarget:self action:@selector(alertTextAction:) forControlEvents