iOS UIAlertController增加输入框UITextField

这篇博客介绍了在iOS应用中如何在UIAlertController上添加UITextField以满足弹窗输入需求。博主分享了两种实现方法,重点讲解了利用UIAlertController直接添加输入框的简便方式,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录今天遇到的一个小需求,要在弹窗上面增加一个输入框,有两种实现方法。

第一种:创建一个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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值