Swift textfield text 1 to 9 if user entered need to show alert

Alexa

New Member
#1
I am trying simple logic, I have only one textfield. into this textfield if user entered 1 to 9 I need to show alert using if.
Mã:
 if (emailTextField.text) < 0 {
            print("100% ")

}
 

Admin

Administrator
Thành viên BQT
#2
UITextField's text is always a String. So if you need to check if it is a particular number, you need to cast the String value to an Int.
Mã:
if let number = Int(emailTextField.text!), (0...9).contains(number) {
    print("100%)
}
Note: You might want to check out the UITextFieldDelegate methods if your logic revolves around text field editing events.
Mã:
yourTextField.addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged)
.
.
.
@objc func valueChanged(_ textField: UITextField) {
    if let number = Int(emailTextField.text!), (0...9).contains(number) {
        print("100%)
    }
}
Sidenote: I have no idea what your use case is for this.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top