如何在 swift 中获取 stackview 文本字段中 otp 动态数字键盘中的数字
how to get number in stackview textfield form dynamic numeric pad for otp in swift
如何在 swift 中获取 stackview 文本字段中 otp 动态数字键盘中的数字
如何在 swift 中获取 stackview 文本字段中动态数字键盘的数字 for otp
如何在 swift
中获取 stackview 文本字段中动态数字键盘的数字
@IBAction func one(_ sender: Any) {
operation = (sender as AnyObject).tag
if (sender as AnyObject).tag == 1 {
textField1.text = "0";
}
if (sender as AnyObject).tag == 2 {
text1.text = "1";
}
if (sender as AnyObject).tag == 3 {
text1.text = "2";
}
if (sender as AnyObject).tag == 4 {
text1.text = "3";
}
if (sender as AnyObject).tag == 5 {
text1.text = "4";
}
if (sender as AnyObject).tag == 6{
text1.text = "5";
}
if (sender as AnyObject).tag == 7 {
text1.text = "6";
}
if (sender as AnyObject).tag == 8 {
text1.text = "7";
}
if (sender as AnyObject).tag == 9 {
text1.text = "8";
}
if (sender as AnyObject).tag == 10 {
text1.text = "9";
}
}
如果您将发件人类型设置为 UIButton
,那么在您的按钮操作中,所有 if 条件都是不必要的,因此您可以直接访问 currentTitle
property of UIButton
. Also instead of trying to get textField from stackView you can make IBOutletCollection
of UITextField
, if you don't know how to male IBOutletCollection
you can follow this medium documentation
。创建一个字符串变量来存储 OTP
并使用它来赋值给 textField
@IBOutlet var otpFields: [UITextField]! //Make sure you add textFields in proper order
var otpString = "" // to assign value to textField.
现在添加一种方法来设置从 otpString 到 textFields 的文本,并像下面这样更改您的按钮操作。
func setupOTPFields() {
for (index,ch) in otpString.enumerated() {
otpFields[index].text = String(ch)
}
}
//action of 0-9 button
@IBAction func one(_ sender: UIButton) {
if otpString.count <= 6 { //Because you only have 6 character otp
otpString += (sender.currentTitle ?? "")
//Call setup field method
self.setupOTPFields()
}
}
//action of c button
@IBAction func cButtonClick(_ sender: UIButton) {
if !otpString.isEmpty {
otpString = String(otpString.dropLast())
//Call setup field method
self.setupOTPFields()
}
}
如何在 swift 中获取 stackview 文本字段中 otp 动态数字键盘中的数字 如何在 swift 中获取 stackview 文本字段中动态数字键盘的数字 for otp 如何在 swift
中获取 stackview 文本字段中动态数字键盘的数字@IBAction func one(_ sender: Any) {
operation = (sender as AnyObject).tag
if (sender as AnyObject).tag == 1 {
textField1.text = "0";
}
if (sender as AnyObject).tag == 2 {
text1.text = "1";
}
if (sender as AnyObject).tag == 3 {
text1.text = "2";
}
if (sender as AnyObject).tag == 4 {
text1.text = "3";
}
if (sender as AnyObject).tag == 5 {
text1.text = "4";
}
if (sender as AnyObject).tag == 6{
text1.text = "5";
}
if (sender as AnyObject).tag == 7 {
text1.text = "6";
}
if (sender as AnyObject).tag == 8 {
text1.text = "7";
}
if (sender as AnyObject).tag == 9 {
text1.text = "8";
}
if (sender as AnyObject).tag == 10 {
text1.text = "9";
}
}
如果您将发件人类型设置为 UIButton
,那么在您的按钮操作中,所有 if 条件都是不必要的,因此您可以直接访问 currentTitle
property of UIButton
. Also instead of trying to get textField from stackView you can make IBOutletCollection
of UITextField
, if you don't know how to male IBOutletCollection
you can follow this medium documentation
。创建一个字符串变量来存储 OTP
并使用它来赋值给 textField
@IBOutlet var otpFields: [UITextField]! //Make sure you add textFields in proper order
var otpString = "" // to assign value to textField.
现在添加一种方法来设置从 otpString 到 textFields 的文本,并像下面这样更改您的按钮操作。
func setupOTPFields() {
for (index,ch) in otpString.enumerated() {
otpFields[index].text = String(ch)
}
}
//action of 0-9 button
@IBAction func one(_ sender: UIButton) {
if otpString.count <= 6 { //Because you only have 6 character otp
otpString += (sender.currentTitle ?? "")
//Call setup field method
self.setupOTPFields()
}
}
//action of c button
@IBAction func cButtonClick(_ sender: UIButton) {
if !otpString.isEmpty {
otpString = String(otpString.dropLast())
//Call setup field method
self.setupOTPFields()
}
}