OGeek|极客世界-中国程序员成长平台

标题: ios - 带有导出集合和标签的注册表单 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:28
标题: ios - 带有导出集合和标签的注册表单

我是 Swift 语言的新手。我有 Web 开发方面的知识,但我需要你的帮助。

我需要创建一个包含导出集合的注册页面。我有 4 个文本字段,并为每个字段分配了标签。当我打算使用beingfirstresponder()将光标从一个文本字段移动到另一个时,它不起作用,当我打印标签时,就像我为它的文本字段分配了一个标签一样,它并不总是打印相同的。

它以随机顺序出现。这是代码。

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet var registerTF: [UITextField]!


@IBAction func registerTFTapped(_ sender: UITextField) {

   let tag = sender.tag
   print(tag)
    for tf in registerTF
    {
     if tf.tag == tag
     {

        func textFieldShouldReturn( _ textField: UITextField) -> Bool {

            nextTextFieldToFirstResponder(textField: textField)

            return true;
        }

        func nextTextFieldToFirstResponder(textField: UITextField) {

            if tf.tag == 0 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 1 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 2 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 3 {

                self.resignFirstResponder()

            }

        }

        }
    }
}

//LifeCycle-Starts

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(_ animated: Bool) {
    //#
}

override func viewDidAppear(_ animated: Bool) {
    //#
}

override func viewWillDisappear(_ animated: Bool) {
    //#
}

override func viewDidDisappear(_ animated: Bool) {
    //#
}

//LifeCycle-Ends

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}



Best Answer-推荐答案


这只有在 TextFields 集合中的顺序等于标签顺序时才有效

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet var registerTF: [UITextField]!

    func nextTag(currentTag:Int)->Int{

        if(currentTag < registerTF!.count - 1){
            return currentTag + 1
        }
        return -1
    }

    func nextTextFieldToFirstResponder(textField: UITextField) {

        let nextTag = self.nextTag(currentTag: textField.tag)

        if(nextTag >= 0){
            self.registerTF![nextTag].becomeFirstResponder()
        }else{
            textField.resignFirstResponder()
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.nextTextFieldToFirstResponder(textField: textField)
        return true
    }

    //LifeCycle-Starts

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        for textField in registerTF {
            textField.delegate = self
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        //#
    }

    override func viewDidAppear(_ animated: Bool) {
        //#
    }

    override func viewWillDisappear(_ animated: Bool) {
        //#
    }

    override func viewDidDisappear(_ animated: Bool) {
        //#
    }

    //LifeCycle-Ends

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

关于ios - 带有导出集合和标签的注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47885040/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4