如何在StackView文本字段中获取数字以快速获取OTP的动态数字填充

时间:2018-08-09 05:59:13

标签: swift uitextfield uistackview

enter image description here

如何在StackView文本字段中快速获取OTP的动态数字填充的数字 如何在StackView文本字段中获取数字以快速获取OTP的动态数字填充 如何在Swift中的otp的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";
    }
}

1 个答案:

答案 0 :(得分:0)

在按钮操作中,如果您将发件人类型设为UIButton,则在不需要条件的情况下,可以直接访问UIButton的{​​{3}}属性。同样,除了尝试从stackView获取textField之外,还可以使IBOutletCollection中的UITextField成为IBOutletCollection,如果您不知道如何使OTP成为男性,则可以遵循此currentTitle。使一个字符串变量存储textField,并使用该变量将值分配给@IBOutlet var otpFields: [UITextField]! //Make sure you add textFields in proper order var otpString = "" // to assign value to textField.

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()
    }
}

现在添加一种方法来将文本从otpString设置为textFields,还可以如下更改按钮操作。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" >
    </head>
    <body>
        <canvas id="canvasToDraw"></canvas>
        <input type="file" onclick="openPicture()"></input>
        <script>
        function openPicture()
        {
            var file = document.querySelector('input[type=file]').files[0];
            var reader  = new FileReader();



            reader.readAsDataURL(file);
            // 
            img = new Image();
            img.id = "bufferPicture";
            document.body.appendChild( img);

            reader.addEventListener('load', function() {                        
                    img.src = ""+reader.result;

            var height = img.height;
            var width = img.width;

            document.getElementById("canvasToDraw").width = width;
            document.getElementById("canvasToDraw").height = height;

            var contextCanvas = document.getElementById("canvasToDraw").getContext("2d");
            contextCanvas.drawImage(    document.getElementById("bufferPicture"), 0, 0  );
            });


            // remove the picture
            // document.body.removeChild(document.getElementById("bufferPicture"));
        }
        </script>
    </body>
</html>