简体   繁体   English

隐藏警报控制器之前检查文本字段中的值-iOS Swift

[英]Checking the value in textfield before alert controller hides - iOS swift

What I am trying to do is that I am taking user's contact number (mobile number) if it is correct then it should continue the signup otherwise the alert controller should not hide and show some error 我想做的是,如果我输入的是正确的用户联系电话(手机号码),则应继续注册,否则警报控制器不应隐藏并显示一些错误

let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
alertController.addTextField {
     (textField) -> Void in
      textField.tag = 128
      textField.delegate = self
      textField.placeholder = "923xxxxxxxxx"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
      alert -> Void in
      let textField = alertController.textFields![0] as UITextField

      if (textField.text?.count)! == 12 && ((textField.text?.substring(to: 3))!) == "923" {
          //my code

      }
      else {
          //should show/keep alert controler
      }
}))             
self.present(alertController, animated: true, completion: nil)

You could disable the button ( UIAlertAction ) by default. 您可以默认禁用按钮( UIAlertAction )。 Then listen to text changes in your UITextField and update the button accordingly: 然后,监听UITextField文本更改,并相应地更新按钮:

var autoEnableAlertAction: UIAlertAction?

func presentAlertController() {
    // alert controller
    let ac = UIAlertController(title: "Registration", message: "Please enter your phone number.", preferredStyle: .Alert)

    // alert action - disabled by default
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    okAction.enabled = false
    ac.addAction(okAction)

    // store a reference to the alert action to enable / disable it later
    autoEnableAlertAction = okAction

    ac.addTextFieldWithConfigurationHandler { tf in
        // listen for changes in the textfield
        tf.addTarget(self, action: #selector(self.checkTextFieldText(_:)), forControlEvents: .EditingChanged)
    }

    presentViewController(ac, animated: true, completion: nil)
}

@objc func checkTextFieldText(textField: UITextField) {
    autoEnableAlertAction?.enabled = textField.text?.characters.count >= 5
}

Sorry for my answer being a Swift 2 version... :) 抱歉我的答案是Swift 2版本... :)

Try out this, 试试这个

let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
alertController.addTextField {
    (textField) -> Void in
    textField.tag = 128
    textField.delegate = self
    textField.placeholder = "923xxxxxxxxx"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
    alert -> Void in
    let textField = alertController.textFields![0] as UITextField
    if (textField.text?.count)! == 12 && ((textField.text?.substring(to: 3))!) == "923"
      {
        //my code
    }
    else
    {
        //alert with error
        let alertControllerError = UIAlertController(title: "error", message: "error here", preferredStyle: .alert)

        alertControllerError.addAction(UIAlertAction(title: "ok", style: .default, handler: { alert -> Void in

        }))
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1)) {
            self.present(alertControllerError, animated: true, completion: nil)
        }
    }
}))
self.present(alertController, animated: true, completion: nil)
func presentAlertController()
{
    let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
    alertController.addTextField {
        (textField) -> Void in
        textField.tag = 128
        textField.placeholder = "923xxxxxxxxx"
    }
    alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
        alert -> Void in
        let textField = alertController.textFields![0] as UITextField

        if (textField.text?.count)! == 12 && textField.text?.prefix(3) == "923"
        {
            print("sucess")

        }
        else
        {
            print("Incorrect Number")


            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
                self.presentAlertController(parameters: parameters)
            }
        }
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{
        alert -> Void in
    }))

    self.present(alertController, animated: true, completion: nil)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从当前警报控制器(或操作表中的文本字段)内部调用警报控制器? Xcode 8,Swift 3,IOS - Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS ios Swift 2:扩展名-带有文本字段的警报 - ios Swift 2: extension - Alert with textfield 从 Swift 中的 iOS 警报中的 TextField 获取输入值 - Get input value from TextField in iOS alert in Swift 标签 - >警报控制器文本字段? 我可以在警报控制器的文本字段中编辑标签中的预先存在的文本吗? Swift 3,Xcode 8,IOS - Label -> Alert Controller Textfield? Can I edit pre-existing text from a label, in a textfield in an alert controller? Swift 3, Xcode 8, IOS 如何在警报控制器的文本字段中设置数字键盘[swift] - How to have a numberpad in a textfield of an alert controller [swift] 在iOS中检查文本字段的日期值 - Checking textfield value for date in iOS 删除单元格Swift Xcode之前的警报控制器 - Alert Controller Before Delete Cell Swift Xcode iOS 15 键盘隐藏文本框 - iOS 15 Keyboard hides textfield 带有文本字段验证警报的UIAlertController在Swift iOS中显示问题 - UIAlertController with textfield validation alert displaying issue in swift ios swift4中textfield的值为0时如何显示提示? - How to display alert when the value of textfield is 0 in swift4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM