iOS 可输入的提示框

作者: 爱编程的小兵 | 来源:发表于2017-11-09 14:38 被阅读0次

先上图,实现效果如下,用的系统最原始提示输入框

输入框.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)

如果你想有两个甚至多个输入框的话可以继续加入

  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 = "随便说点儿什么吧..."
            // 可以设置代理,控制输入的字数以及键盘类型等等
      })
      // 对于第二个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];

相关文章

网友评论

    本文标题:iOS 可输入的提示框

    本文链接:https://www.haomeiwen.com/subject/iswpmxtx.html