在警报中选择一个选项时,如何防止我的文本字段成为第一个响应者?

时间:2018-04-06 20:52:57

标签: ios swift uialertcontroller first-responder

我正在尝试对textFieldShouldEndEditing中的文本字段中的条目进行验证,我检查该值是非数字还是超出范围,然后调用显示警报的函数。显示警报后,我将值设置为默认值,然后调用其他函数来执行计算。

无论我选择哪种操作来解除警报,都会触发编辑文本字段。这是我想要的第一个选项(“再试一次”),但是对于“设置为默认”选项,我只想提醒离开并且不开始编辑文本字段,因为已经分配了默认值。我不明白警报如何与第一响应者状态交互,或者为什么文本字段再次被给予第一响应者状态。相关代码:

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    var InvalidFlagText: String = ""
    let WindIntBool = isStringAnInt(string: textField.text!)
    if WindIntBool { //if entered wt is numeric, check to see if out of bounds of data loaded in table
        if WindInt < LowestWind || WindInt > HighestWind { //value is out of bounds of data, set to default
                txtWind.text = "0"
    //                display alert
                InvalidFlagText = "OutofBounds"
                DisplayAlert(InvalidFlag: InvalidFlagText)
        }
   } else { // if not numeric, set to default value
            txtWind.text = "0"

            //                display alert
            InvalidFlagText = "Nonnumeric"
            DisplayAlert(InvalidFlag: InvalidFlagText)

   }
CalculateResults()
    return true
}


 func DisplayAlert (InvalidFlag: String) {
    var messageText: String = ""
    if InvalidFlag == "Nonnumeric" {
        messageText = "Please enter a numeric value."
    } else if InvalidFlag == "OutofBounds" {
        messageText = "Entered value is outside of the valid numeric range. Please enter a valid numeric value"
    }

       let alert = UIAlertController(title: "That is an invalid entry.", message: "\(messageText)", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Try Again", style: .cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "Set to Default", style: .default, handler: { action in

    }))

    self.present(alert, animated: true)
}

2 个答案:

答案 0 :(得分:1)

尝试在警报中辞职所有响应者

alert.addAction(UIAlertAction(title: "Set to Default", style: .default, handler: { action in
      //// set your text value before ////
      self.view.endEditing(true)
}))

正确或更好的方式:

func DisplayAlert (InvalidFlag: String) {
    self.view.endEditing(true)
    var messageText: String = ""
    if InvalidFlag == "Nonnumeric" {
        messageText = "Please enter a numeric value."
    } else if InvalidFlag == "OutofBounds" {
        messageText = "Entered value is outside of the valid numeric range. Please enter a valid numeric value"
    }

    let alert = UIAlertController(title: "That is an invalid entry.", message: "\(messageText)", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Try Again", style: .cancel, handler: { action in
         self.txtWind.becomeFirstResponder()
    }))
    alert.addAction(UIAlertAction(title: "Set to Default", style: .default, handler: { action in
         /// set default value ////
    }))

    self.present(alert, animated: true)
}

答案 1 :(得分:0)

不确定CalculateResults()方法的作用,我也假设了isStringAnInt方法。下面是您期望的功能的代码。

import UIKit

let kDEFAULT_WIND = "0"

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var txtWind: UITextField!
let HighestWind = 200
let LowestWind = 100
var WindInt = -1
override func viewDidLoad() {
    super.viewDidLoad()
    txtWind.delegate = self
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)

    return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if textField.text == kDEFAULT_WIND{
        return true
    }
    var InvalidFlagText: String = ""
    let WindIntBool = isStringAnInt(s: textField.text!)
    if WindIntBool { //if entered wt is numeric, check to see if out of bounds of data loaded in table
        if WindInt < LowestWind || WindInt > HighestWind { //value is out of bounds of data, set to default
            InvalidFlagText = "OutofBounds"
            DisplayAlert(InvalidFlag: InvalidFlagText)
        }
    } else { // if not numeric, set to default value
        InvalidFlagText = "Nonnumeric"
        DisplayAlert(InvalidFlag: InvalidFlagText)

    }
//        CalculateResults()
    return true
}

func isStringAnInt(s : String) -> Bool{
    if let val = Int(s){
        WindInt = val
        return true
    }
    return false
}
func DisplayAlert (InvalidFlag: String) {
    var messageText: String = ""
    if InvalidFlag == "Nonnumeric" {
        messageText = "Please enter a numeric value."
    } else if InvalidFlag == "OutofBounds" {
        messageText = "Entered value is outside of the valid numeric range. Please enter a valid numeric value"
    }
    let alert = UIAlertController(title: "That is an invalid entry.", message: "\(messageText)", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Try Again", style: .cancel, handler: { action in
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now(), execute: {
            self.txtWind.text = kDEFAULT_WIND
            self.txtWind.becomeFirstResponder()
        })
    }))
    alert.addAction(UIAlertAction(title: "Set to Default", style: .default, handler: { action in
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now(), execute: {
            self.txtWind.text = kDEFAULT_WIND
            self.txtWind.resignFirstResponder()
        })
    }))
    self.present(alert, animated: true)
}

}